Iterate through multiple collections in the same “for” loop?

前端 未结 4 1315

I wonder if there\'s such a way to iterate thru multiple collections with the extended for each loop in java.

So something like:

for (Object element          


        
4条回答
  •  余生分开走
    2020-12-17 11:15

    If your lists have the same length, just use the raw for loop:

    Object[] aNum = {1, 2}; 
    Object[] aStr = {"1", "2"}; 
    
    for (int i = 0; i < aNum.length; i++) {
        doSomeThing(aNum[i]);
        doSomeThing(aStr[i]);
    }
    

    It works!

提交回复
热议问题