Problem with adding to list inside a loop in java
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:
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class Trial{
public static void main(final String[] args){
final List<int[]> G = new LinkedList<int[]>();
final List<int[]> New 开发者_JS百科= new LinkedList<int[]>();
final 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");
}
}
}
}
}
And this is the result I got after running:
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!!!!
if I'm not mistaken this is a classical error in Java
your not creating new objects !!! so your always adding and modifying the same one ego in the end your list contains one 1 object over and over again.
wha tyou want is to move
int [] f = new int [2];
into your for loop
It doesn't add multiple copies, it allways add f as a new list item after first iteration you have f then f,f then f,f,f then f,f,f,f in your list. And you are allways modifing your only array f so all elements conatin the same two numbers
You should change:
for(int j=0; j<2; j++){
f[0] = i;
to
for(int j=0; j<2; j++){
f = new int [2];
f[0] = i;
I don't understand your condition
if(!(G.contains(f))){
f is never in the G because you never put it there, so no possible chance it could get there
I assume you are new to java. And I also assume that this is a sample program, or some sort of an exercise.
You want to check whether a list contains an array. You are using list.contains
which is correct.
The problem is how you are constructing the array.
In the following snippet
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;
list.add(f);
}
}
there is only one array you are constructing, outside the for
loop.
You are actually adding to the same array again and again.
And adding this array again and again to the list. The list will contain 4 elements, but all 4 will refer to the same array.
If you want the list to contain different elements, then you have to create them first:
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
int [] f = new int [2]; //<-- new array everytime
f[0] = i;
f[1] = j;
list.add(f);
}
}
Coming to your contains check:
Your check will always be false, because the list G
will always be empty (from the code you have given) you are not adding anything to it.
Since G.contains...
check will always be false
(G
contains nothing), all the elements will be added to New
.
If you want to have some sample code to understand contains, you might want to add something to G
first.
for(int i=0; i<2; i++)
{
int [] f = new int [2]; //<-- new array everytime
f[0] = i;
f[1] = i;
G.add(f);
}
//G will now contain [0,0] and [1,1]
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
int [] f = new int [2]; //<-- new array everytime
f[0] = i;
f[1] = j;
if(!G.contains(f))
{
New.add(f); //Will add only [0,1] and [1,0]
}
}
}
Final notes: In java, it is conventional to have lowercase variable names.
Perhaps not very importane for a sample program, but try to have meaningful variable names. Names like f
and G
don't convey much.
精彩评论