java foreach skip first iteration

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

    I wouldn't call it elegant, but perhaps better than using a "first" boolean:

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

    Other than that, probably no elegant method.  

提交回复
热议问题