java foreach skip first iteration
Use Guava Iterables.skip()
.
Something like:
for ( Car car : Iterables.skip(cars, 1) ) {
// 1st element will be skipped
}
(Got this from the end of msandiford's answer and wanted to make it a standalone 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.
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
for (Car car : cars)
{
if (car == cars[0]) continue;
...
}
Elegant enough for me.