java foreach skip first iteration

Amir Afghani picture Amir Afghani · Apr 19, 2011 · Viewed 68.6k times · Source

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 work for rest
   .
   .
}

Answer

Sean Adkinson picture Sean Adkinson · Apr 19, 2011

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.