配列がリストにないかどうかをチェックするコードを書いた後、それを別のリストに追加する必要があります。私はこれにリンクされたリストを使用しました。次のように私のコードは次のとおりです:Javaのループ内でリストに追加するときの問題
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 = 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");
}
}
}
}
}
そして、これは私が実行した後に得た結果である。しかし、私の問題は、プログラムは常に、現在の配列の複数のコピーを追加し、前のリストに内部の何削除され
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
助けてください!!!!
うんうん。はい、条件((G.contains(f))) が実際に何もしていない場合は、この問題を明確にするためにコードの一部を削除しました。どうもありがとう。 – aminx