Amir Afghani Amir Afghani - 18 days ago 8
Java Question

java foreach skip first iteration

Is there an elegant way to skip the first iteration of the new Java5 foreach loop ?

Example pseudo-code:

for ( Car car : cars ) {
//skip if first, do work for rest
.
.
}

Answer

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.