6

I'm reading in a file and storing it in t1. How do I access the elements in t1? When I try to print it I get addresses instead of values. Also whats 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
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
nubme
  • 2,857
  • 8
  • 29
  • 25

7 Answers7

21

String[] is an array of strings, hence the reason it is not printing as you would expect, 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 concise:

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));
}
DaveJohnston
  • 10,031
  • 10
  • 54
  • 83
4

As Petar mentioned, your list is a List of Arrays of Strings, so you are printing out the array, not the array contents.

A lazy way to print out the array contents is to convert the array to a List<String> with java.utils.Arrays.toString():

String[] stringArray=new String[] { "hello", world };
System.out.println(Arrays.toString(stringArray));

gives

["hello","world"]

RedPandaCurios
  • 2,264
  • 12
  • 20
2

You print a List with arrays. While the List classes overload the toString() method to print each element, the array uses the default toString used by Object which only prints the classname and the identity hash.

To print all you either have to iterate through the List and print each array with Arrays.toString().

for(String[] ar:t1)System.out.print("["+Arrays.toString(ar)+"]");

Or you put each array into a List

List<List<String>> tt1 = new ArrayList<List<String>>();
for(String[] ar: t1)tt1.add(Arrays.asList(ar));//wraps the arrays in constant length lists
System.out.println(tt1)
josefx
  • 15,506
  • 6
  • 38
  • 63
1

You can print the list content by simply converting the list in to array first and then using Arrays.toString like below

System.out.println(Arrays.toString(your_list.toArray());
josliber
  • 43,891
  • 12
  • 98
  • 133
Ankit
  • 67
  • 4
0

String[] is an array of strings. You print the address of the array - when you see [L that means an array. You have to iterate through the elements of every array, instead of printing the address:

String[] temp = t1.get(i);
for (int j = 0; j < temp.length; j++)
    System.out.println(temp[j]);
Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
0

As I said in your other question, when creating your own Record object, you can implement the toString() function for returning a reasonable representation of that piece of information.

Community
  • 1
  • 1
Marc
  • 3,550
  • 22
  • 28
0

Using streams, Java 8 and above:

List<String> l = 
    reader.readAll() //List<String[]>
          .stream()
          .flatMap(Arrays::stream) //List<Stream<String>> => Stream<String>
          .collect(Collectors.toList());

Arrays.stream() takes an array and produces a stream.
flatMap() lets you replace each value of a stream with another stream and then concatenates all the generated streams into a single stream.

jumping_monkey
  • 5,941
  • 2
  • 43
  • 58