简体   繁体   中英

print to the console all the items in the ArrayList<String[]>

I have declared an ArrayList of a generic string array

ArrayList<String[]> dataListCol = new ArrayList<String[]>();

I am populating the arraylist this way

for (int i = 0; i < jArray.length(); i++) {
                        JSONObject json_data = jArray.getJSONObject(i);
                        String[] strArrayCol = new String[2];
                        strArrayCol[0] = json_data.getString("col1");
                        strArrayCol[1] = json_data.getString("col2");
                        dataListCol.add(strArrayCol);

                       }

I am attempting to print all the individual items in the array as shown

System.out.println("new array " + Arrays.toString(dataListCol.toArray()));

the output is not a meaningful string representation. Please how can I print all the items in the array

As each element itself is an array , you need to call Arrays.toString on each element to print the values, eg:

List<String[]> list = new ArrayList<String[]>();
list.stream()
.map(Arrays::toString)
.forEach(System.out::println);

Or, to print just one element, simply use the following inside the loop:

System.out.println(Arrays.toString(strArrayCol));

Update

If you want to assign the String to a reference instead of printing it then you can use Collector , eg:

String string = list.stream()
.map(Arrays::toString)
.collect(Collectors.joining("|"));

This will give you pipe separated string for all the elements.

Two way to do that, loop or foreach method.

for (String object: list) {
  System.out.println(object);
}

or

ArrayList l=new ArrayList();
l.add(value);
l.forEach((a)->System.out.println(a));

hope that help you

The fact is that you are calling the method tostring() on the array that use the method for generic objects showing to you the hash of that specific object.

You can simply do something like this

    int size = t.length; 
for (int i=0; i<size; i++) { System.out.println(t[i]); 
}

Or in a simpler way you can use

Arrays.toString() 

And

System.out.println(Arrays.toString(yourArray));

What's Actually Happening

You've made a list that holds type String[] , when you ask the list to return to you it's state represented by an array, the call to toArray() , it returns an array that contains each element in the list.

So the array you've received back is an Object[] that contains all the elements in the list. Remember what type of elements you declared the list to hold - String[]

Arrays.toString(Object[] array) iterates through the array given calling toString() on each element. The runtime type of each element in the array you gave is String[] , therefore on every iteration toString() is being called on an array.

What You Want to Happen

You want to call toString() on each String element in each array of String that the list is holding:

for(String[] strArray : dataListCol) {
  System.out.println(Arrays.toString(strArray));
}

or if your a fan of spaghetti output :

System.out.println(Arrays.deepToString(dataListCol.toArray()));

Note Arrays.deepToString(dataListCol.toArray())) does what you wanted. This is because when focusing only on how elements are stored, essentially a List<Object[]> is equivalent to a 2D array, Object[][] , just as a List<Object> is equivalent to a single dimension array Object[] .

Except elements of an array are stored contiguously in memory whereas Collection classes do not store their elements contiguously (just to keep the memory heroes off my back)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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