배열이 목록에 없는지 확인하는 코드를 작성한 다음 다른 목록에 추가해야합니다. 나는 이것을 위해 연결된 목록을 사용했다. 다음과 같이 내 코드는 다음과 같습니다 :자바의 루프 내부에 목록을 추가 할 때의 문제
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