java foreach skip first iteration

前端 未结 11 965
刺人心
刺人心 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:46

    With new Java 8 Stream API it actually becomes very elegant. Just use skip() method:

    cars.stream().skip(1) // and then operations on remaining cars
    

提交回复
热议问题