java foreach skip first iteration

前端 未结 11 923
刺人心
刺人心 2021-02-01 12:05

Is there an elegant way to skip the first iteration in a Java5 foreach loop ?

Example pseudo-code:

for ( Car car : cars ) {     
   //skip if first, do w         


        
11条回答
  •  囚心锁ツ
    2021-02-01 12:52

    SeanA's code has a tiny error: the second argument to sublist is treated as an exclusive index, so we can just write

    for (Car car : cars.subList(1, cars.size()) {
       ...
    }
    

    (I don't seem to be able to comment on answers, hence the new answer. Do I need a certain reputation to do that?)  

提交回复
热议问题