简体   繁体   English

如何显示对象的数组列表?

[英]How to display an arraylist of objects?

i wrote a code that takes a line from a txt file, splits it into different strings and integers and then stores it into an array list as an object called professor.我编写了一个代码,它从 txt 文件中取出一行,将其拆分为不同的字符串和整数,然后将其存储到一个数组列表中作为一个名为“教授”的对象。 The code of the main class its this:主类的代码是这样的:

public class Main {

    public static void main(String[] args) throws IOException {
        FileReader file = new FileReader("text.txt");
        BufferedReader reader = new BufferedReader(file);
        ArrayList<Profesor>professors = new ArrayList<Profesor>();
        String line = reader.readLine();
        String[] lineSplit = new String[11];

        while(line != null){
            lineSplit = line.split("\\s+");
            professors.add(new Profesor(lineSplit[0], lineSplit[1], Integer.parseInt(lineSplit[2]), Integer.parseInt(lineSplit[3]), Integer.parseInt(lineSplit[4]), Integer.parseInt(lineSplit[5]), Integer.parseInt(lineSplit[6]), Integer.parseInt(lineSplit[7]), Integer.parseInt(lineSplit[8]), Integer.parseInt(lineSplit[9]), Integer.parseInt(lineSplit[10]), Integer.parseInt(lineSplit[11])));                     
        }

    }

}

And the Profesor class its:而 Profesor 类是:

public class Profesor {
    private String name;
    private String subject;
    private int wh0;
    private int wh1;
    private int wh2;
    private int wh3;
    private int wh4;
    private int wh5;
    private int wh6;
    private int wh7;
    private int wh8;
    private int wh9;    
    public Profesor(String n, String s, int w0, int w1, int w2, int w3, int w4, int w5, int w6, int w7, int w8, int w9){
        name = n;
        subject = s;
        wh0 = w0;
        wh1 = w1;
        wh2 = w2;
        wh3 = w3;
        wh4 = w4;
        wh5 = w5;       
        wh6 = w6;
        wh7 = w7;
        wh8 = w8;
        wh9 = w9;               
    }
}

And the txt file its something like:和 txt 文件类似:

Jhon Maths 173 486 789 954 684 235 446 168 749 851   
Robert MathsII 283 686 948 978 144 224 473 468 778 845

The question its how can I display the arraylist into the console?问题是如何将数组列表显示到控制台中? and how do I access a string inside one of the objects inside of the arraylist?以及如何访问数组列表中的一个对象内的字符串?
Thanks in advance提前致谢

You can use get() method for each ArrayList .您可以对每个ArrayList使用get()方法。 So typing professors.get(0) will return the first Professor object.所以输入professors.get(0)将返回第一个Professor对象。

Not only that once you get that object, you need to create something called getName , or getObject .不仅如此,一旦您获得该对象,您还需要创建一个名为getNamegetObject东西。 It's because your variables are private and a class outside of Professor is trying to access the private variable.这是因为您的变量是私有的,而教授之外的一个类正试图访问私有变量。

It looks something like this:它看起来像这样:

public String getName() {
    return name;
}

And once you have that method inside Professor class, you can call it within your Main class by calling一旦您在Professor类中拥有该方法,您就可以通过调用在Main类中调用它

Profesor p = professors.get(0);   // returns the first Profesor inside professors ArrayList
String professorName = p.getName();   // returns the name variable of the above professor

More about ArrayList can be found here:可以在此处找到有关ArrayList更多信息:

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int) http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int)

Here's the thing: when you try to print an object, Java figures out what to print by calling the object's toString() function.事情是这样的:当您尝试打印一个对象时,Java 通过调用该对象的toString()函数来确定要打印的内容。 So you need to tell Java that by implementing that particular function in your Professor object:因此,您需要通过在您的教授对象中实现该特定功能来告诉 Java:

public String toString()
{
    return name + subject + Arrays.toString(wh); 
    // put all those wh numbers into an array!

}

This function returns a String of the form:此函数返回以下形式的字符串:

Jhon Maths 173 [486, 789, 954, 684, 235, 446, 168, 749, 851] which is what Java prints out. Jhon Maths 173 [486, 789, 954, 684, 235, 446, 168, 749, 851]这是 Java 打印出来的。

Now, you can iterate through your ArrayList of Professors and simply System.out.println them one by one.现在,您可以遍历教授的ArrayList并简单地System.out.println一一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM