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

后端 未结 7 1529
萌比男神i
萌比男神i 2021-01-19 04:55

E.g.

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

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

suppose we are sure that list1.size() equals list2

7条回答
  •  臣服心动
    2021-01-19 05:12

    You can't traverse two lists at the same time with the enhanced for loop; you'll have to iterate through it with a traditional for loop:

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

提交回复
热议问题