Hi all,
I wrote a code to check if an array is not in a list, then it should add it to another list. I used a linked list for this. But my problem is the program always add multiple copies of the current array and remove what's inside in the list before: My code is as follows:
And this is the result I got after running:import java.util.Arrays; import java.util.LinkedList; import java.util.List; public class Trial { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub List<int[]> G = new LinkedList<int []>(); List<int []> New = new LinkedList<int []>(); int [] f = new int [2]; for(int i=0; i<2; i++){ for(int j=0; j<2; j++){ f[0] = i; f[1] = j; //System.out.println("f is "+Arrays.toString(f)); if(!(G.contains(f))){ System.out.println("current f is "+Arrays.toString(f)); // I print here in order to see what is f New.add(f); System.out.println("content of the list New"); // I print the list New to see its contents for(int k=0; k<New.size(); k++){ System.out.println(Arrays.toString(New.get(k))); } System.out.println("finished printing the list New"); } } } } }
current f is [0, 0]
content of the list New
[0, 0]
finished printing the list New
current f is [0, 1]
content of the list New
[0, 1]
[0, 1]
finished printing the list New
current f is [1, 0]
content of the list New
[1, 0]
[1, 0]
[1, 0]
finished printing the list New
current f is [1, 1]
content of the list New
[1, 1]
[1, 1]
[1, 1]
[1, 1]
finished printing the list New
Please help!!!!