Skip to content
Advertisement

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

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) ?

Advertisement

Answer

for(int i = 0; i< list1.size(); i++){
  String str1 = list1.get(i);
  String str2 = list2.get(i);
  //do stuff
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement