ArrayList 내부에 저장된 int의 인덱스를 어떻게 찾을 수 있습니까?ArrayList 인덱스?
public static void thisClass(int max, int[] queue1, ArrayList<ArrayList<Integer>> aList) {
int[] anArr = Arrays.copyOf(queue1, queue1.length);
Arrays.sort(anArr);
... //sorts ints into groups (ArrayList of ArrayLists)
}
int n = 0;
for (ArrayList<Integer> t : newList) {
String itemS = "";
int total = 0;
for (int index : t) {
int value = index; //isn't returning index of arrayList - its returning the value
if (!itemS.isEmpty()) {
itemS += ", ";
}
itemS += index + ":" + value; //want it to output "element Position : element Value
total += value;
}
}
}
+1. 또는 변수를 0으로 초기화하고 반복 할 때마다 증가시킬 수 있습니다. LinkedList의 경우 get()은 O (1) 대신 O (N)입니다. –
이것은 가장 합리적인 것 같습니다. 그러나 나는 for-each 루프를 사용하는 것을 선호 할 것 같다. 나는이 경우에 인덱스를 추적하지 않기로 결정했다. 다음에 나는 for 루프를 대신 구현할 것이다. 감사. – binary101