0

I am trying to make pairs of elements in an array and store them in an ArrayList (al variable in below code). Then store those ArrayList objects in an ArrayList of ArrayList (variable finalList in below code) and then print finalList.

I am able to get my output if I only use ArrayList and not finalList, but I want to know what is going wrong in my code. Why is it not displaying me the proper output?

import java.util.ArrayList;

public class Test {
    static int[] arr = {2,4,6,8,10};
    static ArrayList<Integer> al = new ArrayList<Integer>();
    static ArrayList<ArrayList> finalList = new ArrayList<ArrayList>();

    public static void makePairs() {
        for(int i = 0; i < arr.length - 1; i++) {
            al.clear();
            al.add(arr[i]);
            for(int j = i + 1; j < arr.length; j++) {
                al.add(arr[j]);
//                System.out.print(al);
                finalList.add(al);
                al.remove(1);
            }
//            System.out.println();
            System.out.println(finalList);
            finalList.clear();
        }
    }

    public static void main(String[] args) {
        makePairs();
    }
}

This is the output I am getting.

enter image description here

The output I am expecting is:

[[2,4], [2,6], [2,8], [2,10]]
[[4,6], [4,8], [4,10]]
[[6,8], [6,10]]
[[8,10]]

Please help me resolve my problem.

Abra
  • 19,142
  • 7
  • 29
  • 41
JavaBoi
  • 37
  • 4

3 Answers3

0

Refer to Is Java "pass-by-reference" or "pass-by-value"?.

You are adding the same ArrayList to finalList. I suggest that you verify this by running your code with the debugger of your IDE. You need to create a copy of al and add the copy to finalList.

There are many ways to make a copy. Refer to How to clone ArrayList and also clone its contents?. In the below code, I use the "copy" constructor of class java.util.ArrayList.

import java.util.ArrayList;

public class Test {
    static int[] arr = {2, 4, 6, 8, 10};
    static ArrayList<Integer> al = new ArrayList<>();
    static ArrayList<ArrayList<Integer>> finalList = new ArrayList<>();

    public static void makePairs() {
        for (int i = 0; i < arr.length - 1; i++) {
            al.clear();
            al.add(arr[i]);
            for (int j = i + 1; j < arr.length; j++) {
                al.add(arr[j]);
//                System.out.print(al);
                finalList.add(new ArrayList<>(al)); // CHANGE HERE
                al.remove(1);
            }
//            System.out.println();
            System.out.println(finalList);
            finalList.clear();
        }
    }

    public static void main(String[] args) {
        makePairs();
    }
}

Note that I only changed one line of the code in your question.

When I run the above code, I get the following output:

[[2, 4], [2, 6], [2, 8], [2, 10]]
[[4, 6], [4, 8], [4, 10]]
[[6, 8], [6, 10]]
[[8, 10]]
Abra
  • 19,142
  • 7
  • 29
  • 41
0

i think because your "al" array is static, when you add it to "finalList" it still a static variable and still can be modified, so when you remove al(1), it will remove the element from the static "al" in the FinalList. In this case you can try add a clone of "al".

finalList.add((ArrayList)al.clone());
al.clear();
vhg2901
  • 11
  • 4
0

I realized that when I was adding my ArrayList al to the 'ArrayList of ArrayList' that I created which I called finalList, it was not passing a new copy of al ArrayList but the reference to it.

I thought that it would pass a new copy as they say Java is Pass By Value but I see it's not the same case with arrays and ArrayList, we we pass them as arguments, there references are passed.

I found my solution by using the copy constructor of the ArrayList as suggested by @Abra.

Here is the updated source code.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;

public class Test {
    static int[] arr = {2, 4, 6, 8, 10};
    static ArrayList<Integer> al = new ArrayList<>();
    static ArrayList<ArrayList> finalList = new ArrayList<>();

    public static void makePairs() {
        for (int i = 0; i < arr.length - 1; i++) {
            al.clear();
            al.add(arr[i]);

            for (int j = i + 1; j < arr.length; j++) {
                al.add(arr[j]);
            
            /* 
            int[] array = al.stream().mapToInt(k -> k).toArray();
            
            ArrayList<Integer> newlist = (ArrayList<Integer>) Arrays.stream(array).boxed().collect(Collectors.toList());
            
            finalList.add(newlist);
            */

                finalList.add(new ArrayList<>(al)); // CHANGE HERE

                al.remove(1);
            }

            System.out.println(finalList);
            finalList.clear();
        }
    }

    public static void main(String[] args) {
        makePairs();
    }
}
nakhodkin
  • 1,327
  • 1
  • 17
  • 27
JavaBoi
  • 37
  • 4