Home > other >  Can pairs of element in an array converted and displayed in arraylist of arraylist?
Can pairs of element in an array converted and displayed in arraylist of arraylist?

Time:12-11

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.

CodePudding user response:

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]]

CodePudding user response:

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();
  • Related