Unable to display ArrayList

I am doing a heavy operation with arrayList from array lists. when I try to display a list of parent arrays, I have a problem with displaying empty arraylists, as shown below

The declaration of myListList is listed below.

List<List<String>> arrayList = new ArrayList<List<String>>(); 

Here is a list of my children

 List<String> stringLine = new ArrayList<String>(); 

In my program, I add this LineLine to an array like this

 arrayList.add(stringLine); 

My conclusion when trying to print the parent array of List the print size of the array of List is still 44: [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]

Note. In the above output 44 there are no array elements that it has

When I try to print a child, it displays a good

 StringLine is : [1640, 1138, 878, 1600, 1978, 280, 24, 2509, 702, 553, 2362, 2019, 1558, 2494, 823, 35, 1181, 1915, 1261, 1448, 493, 798, 1160, 651, 2249, 1639, 2428, 458, 2556, 939, 2114, 2339, 2373, 286, 2078, 844, 2673, 1486, 1657, 1531, 1043, 734, 2247, 2121, 75, 2599, 975, 29, 175, 960, 2151, 480, 868, 2627, 1941, 671, 2529, 1952, 1623, 2160, 2298] 

Where am I wrong?

UPDATE:

Here is my code snippet in which I add a list to the parent array of List

 private void splitString(String temp) { System.out.println("In splitString method.."); List<String> stringLine = new ArrayList<String>(); StringTokenizer stringTokenizer = new StringTokenizer(temp, " "); while (stringTokenizer.hasMoreTokens()) { stringLine.add(stringTokenizer.nextToken()); } if (stringLine.size() != 3) { arrayList.add(stringLine); } stringLine.clear(); } 
+4
source share
4 answers

Update

After publishing the code, the error is obvious. As you can see here List # clear will delete all items in the List . Since you intentionally delete them, it should not be surprising that the output is empty.

Remove this line: stringLine.clear(); and everything should be fine. You pass the List<String> to the arrayList.add link, which means that it will still point to the same stringLine that you are clearing. If you really need to clean, you need to deeply clone.

You need to set the for in loops correctly and everything will work fine:

 List<List<String>> arrayList = new ArrayList<List<String>>(); List<String> stringLine = new ArrayList<String>(); stringLine.add("5"); stringLine.add("7"); arrayList.add(stringLine); for (List<String> list : arrayList) {// each list in the arrayList for (String current : list) {// each element in each list System.out.println(current); } } 

Or, if you want to immediately print the entire List :

 for (List<String> list : arrayList) {// each list in the arrayList System.out.println(list); // will work as expected. } 
+5
source

This means that in the end you clear all the elements from stringLine . Below is a test case

 List<List<String>> arrayList = new ArrayList<List<String>>(); List<String> stringLine = new ArrayList<String>(); stringLine.add("1"); stringLine.add("2"); List<String> stringLine1 = new ArrayList<String>(); stringLine1.add("3"); stringLine1.add("4"); arrayList.add(stringLine); arrayList.add(stringLine1); stringLine1.clear(); // check this stringLine.clear(); System.out.println(arrayList); 

output: [[], []]

+1
source

Even if people already answered this, they did not explain why this was happening.

Java has references to values ​​like C / C ++. Therefore, when you create a new ArrayList (), it will create a new ArrayList for you somewhere in memory. But if you use the = operator and assign it to another variable, it will simply point to that place in memory.

 ArrayList a = new ArrayList(); ArrayList b = a; //Both a and b point to the same thing here. 

From this point forward, adding or removing elements in is the same as adding or removing to b. Since you use the same ArrayList in the same memory location for all your operations, when you clear () the ArrayList, everything gets cleared.

An easy way to notice what your code is doing is to simply print it when you add things to the ArrayList from ArrayLists.

 private void splitString(String temp) { System.out.println("In splitString method.."); List stringLine = new ArrayList(); StringTokenizer stringTokenizer = new StringTokenizer(temp, " "); while (stringTokenizer.hasMoreTokens()) { stringLine.add(stringTokenizer.nextToken()); } if (stringLine.size() != 3) { arrayList.add(stringLine); System.out.println(arrayList); //DEBUG LINE HERE } stringLine.clear(); } 
+1
source

I assume stringLine is a string []. Therefore, you need to either execute an array loop (as in @ alex23's answer) or use CollectionUtils # addAll , for example:

 CollectionUtils.addAll(arrayList, stringLine); 
0
source

Source: https://habr.com/ru/post/1480515/


All Articles