다음 prob로 고정되었습니다. 대부분의 코드를 무시합니다. 문제는 다음과 같습니다. 모든 반복마다 다른 목록 x 개가 있고 2DarrayList conVert에서 구문 분석하려고하므로 conVert.add (x); 하지만 내 문제는 x.clear();입니다. 내가 분명히하기 전에 conVert를 인쇄 할 때 conVert는 괜찮지 만, x와 clear를 사용하여 인쇄하면 conVert가 지워집니다. 이것으로 무엇을 할 수 있습니까? 사전2D arraylist에 목록 추가
List<Integer> x = new ArrayList<Integer>();
// List<Integer> x1 = new ArrayList<Integer>();
List<Integer> valenceList = new ArrayList<Integer>();
List<List<Integer>> conVert = new ArrayList<List<Integer>>();
for (int q = 1; q <= 4; q++) {
for (int i = 0; i < Faces.length; i++) {
for (int j = 0; j < 2; j++) {
if (Faces[i][j] == q) {
x.add(Faces[i][0]);
x.add(Faces[i][1]);
x.add(Faces[i][2]);
}
}
}
removeDuplicateWithOrder((ArrayList) x);
valenceList.add(x.size() -1);
//x1 = x;
conVert.add(x);
System.out.println(conVert);
x.clear();
//System.out.println(conVert);
}
오른쪽 위 'conVert.add (x.clone()); '로 몇개의 문자를 저장할 수 있습니다 만, 이것은'x'를'ArrayList'로 타입하는 것을 의미합니다. –
@Daniel : 'conVert.add ((ArrayList) x.clone());'와 같이 캐스팅해야합니다. 나는'x'의 구조를 첫 번째 루프로 옮기고 지움을 제거하면 가장 많은 문자를 절약 할 수 있다고 주장 할 것이다. – WhiteFang34
말도 안돼, 그냥'l.getClass(). getMethod ("clone"). invoke (l);':-) –