In Java, how to traverse two lists at the same time?

Tags:



E.g.

for(String str : list1) {
...
}

for(String str : list2) {
...
}

suppose we are sure that list1.size() equals list2.size(), how to traverse these two lists in one for statement?

Maybe something like for(String str1 : list1, str2 : list2) ?

Answer

for(int i = 0; i< list1.size(); i++){
  String str1 = list1.get(i);
  String str2 = list2.get(i);
  //do stuff
}


Source: stackoverflow