Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot display ArrayList

Tags:

java

I am doing a heavy operation with arrayList of arrayLists. when I am trying to display the parent array list, I have a problem that it's displaying empty arraylists as below

My arrayList declaration is hereunder

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

Here is my child list

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

In my program I am adding this stringLine to arrayList like this

arrayList.add(stringLine);

My output when trying to print parent arrayList printing size of arrayList so far 44 content is : [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]

Note: In the above output 44 is the no of arrayList elements it has

When I try to print the child it displays 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, I was wrong?

UPDATE:

Here is my code snippet, where in I am adding a list to the parent arrayList

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();
}
like image 297
srk Avatar asked Mar 23 '23 23:03

srk


2 Answers

Update

After posting your code, the error is obvious. As you can see here List#clear will remove all the elements in your List. Since you are intentionally removing them, it should come as no surprise the output is empty.

Remove this line: stringLine.clear(); and everything should be fine. You are passing the List<String> by reference with arrayList.add, which means it will still point to the same stringLine you are clearing. If you really need to clear you need to deep-clone.

You need to nest the for in loops properly 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 print an entire List at once:

for (List<String> list : arrayList) {// each list in the arrayList
    System.out.println(list); // will work as expected.
}
like image 182
flavian Avatar answered Apr 06 '23 02:04

flavian


Which means at the end you are clearing all elements from stringLine. The following is the 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 : [[], []]

like image 26
vels4j Avatar answered Apr 06 '23 03:04

vels4j