It may be a dummy question but I could not find an answer to that. Is that possible to pass 2 parameters to an enhanced Java for loop? something like:
List A = [1, 2, 3]
List B = [a, b, c]
for (int i: A ; String a in B) {
do something
}
It may be a dummy question but I could not find an answer to that. Is that possible to pass 2 parameters to an enhanced Java for loop? something like:
List A = [1, 2, 3]
List B = [a, b, c]
for (int i: A ; String a in B) {
do something
}
You can loop through both arrays with a normal for loop and access both with the same index
int[] A = new int[]{1, 2, 3};
String[] B = new String[]{"a", "b", "c"};
for (int i = 0; i < A.length; i++) {
System.out.println(A[i]);
System.out.println(B[i]);
}
No you can't pass two variables to same enhanced for loop , to achieve what you need to do use the iterator interface which is fallen under java.util package . By using iterator and it's methods of boolean hasNext() and Object next() you can achieve this.
SOLUTION:
Iterator iteratorA = A.iterator();
Iterator iteratorB = B.iterator();
while(iteratorA.hasNext() && iteratorB.hasNext()) {
valueA = iteratorA.next();
valueB = iteratorB.next();
//other logics
}