Java – print and access lists

I am reading the file and storing it in T1 How do I access elements in T1? When I try to print it, I get an address instead of a value What is the difference between string and string []?

CSVReader reader = new CSVReader(new FileReader("src/new_acquisitions.csv"));
        List <String[]> t1 = reader.readAll();

        int i = 0
        while(i < t1.size()) {
          System.out.println(t1.get(i));
          i++;
        }

Output:

[Ljava.lang.String;@9304b1
[Ljava.lang.String;@190d11
[Ljava.lang.String;@a90653
[Ljava.lang.String;@de6ced

Solution

String [] is a string array, so it does not print as you expected. Please try:

for (int i = 0; i < t1.size(); i++) {
    String[] strings = t1.get(i);
    for (int j = 0; j < strings.length; j++) {
        System.out.print(strings[j] + " ");
    }
    System.out.println();
}

Or more succinctly:

for (String[] strings : t1) {
    for (String s : strings) {
        System.out.print(s + " ");
    }
    System.out.println();
}

Or better yet:

for (String[] strings : t1) {
    System.out.println(Arrays.toString(strings));
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>