Improved for launching the launcher through the list

I am new to Java and I have this doubt. Is it possible to use the Enhanced for loop in Java on an ArrayList, but start at the specified point, and not with ArrayList [0].

For eg. ArrayList<Integer> calc = new ArrayList<Integer>; // calc contains {0,1,2,3,4,5,6,7} 

Can I use advanced for loop and start iteration with calc [2], not calc [0] ?? If possible, how can I do this? In my particular case, using an extended loop cycle would be better than a normal loop.

+7
source share
5 answers

The best way in Java:

 for (Integer i : calc.subList(start, calc.size()) { ... } 

subList is an efficient view of the source list, so you almost certainly need it.

UPDATE

OK, motivated by Mikera's comments, I compared it on jmh . This is the control code:

 import org.openjdk.jmh.annotations.GenerateMicroBenchmark; public class Benchmark1 { static final List<Integer> list = new ArrayList(asList(1,2,3,4,5,6,7,8,9,10)); static { for (int i = 0; i < 5; i++) list.addAll(list); } @GenerateMicroBenchmark public long testIterator() { long sum = 0; for (int i : list) sum += i; return sum; } @GenerateMicroBenchmark public long testIndexed() { long sum = 0; for (int i = 0; i < list.size(); i++) sum += list.get(i); return sum; } @GenerateMicroBenchmark public long testSublistIterator() { long sum = 0; for (int i : list.subList(1, list.size())) sum += i; return sum; } @GenerateMicroBenchmark public long testIndexedSublist() { long sum = 0; final List<Integer> l = list.subList(1, list.size()); for (int i = 0; i < l.size(); i++) sum += l.get(i); return sum; } } 

And here are the results:

 Benchmark ops/msec ------------------------- Indexed 1860.982 IndexedSublist 1642.059 Iterator 1818.657 SublistIterator 1496.994 

Conclusions:

  • extended for the main list, it performs as fast as the indexed iteration, once after the initialization cost;

  • subscriber traversal is somewhat slower than the main list, and iteration is somewhat slower than the indexed traversal;

  • all differences are insignificant for all practical purposes.

+16
source

You are stuck using the traditional loop here ...

 for (int i = 2; i < calc.size(); i++) { Integer x = calc.get(i); } 

Well, if you do not want to create a temporary subList just to use the extended for loop, this is good because the sublists are views of the original list and do not create a new list object:

 for (Integer x : calc.subList(2, calc.size())) { } 
+4
source
 for(Item e : list.subList(1, list.size())) { // ... } 
+3
source

You can iterate through subscriptions as shown below:

 for (Integer integerMember : calc.subList(2, calc.size()) { // operation here } 
+1
source

It should be noted that a reinforced for loop is less efficient for ArrayList and then standard indexing. This is due to the fact that we must create an Iterator object and call it hasNext and next at each step of the loop, causing unnecessary overhead.

For this reason, I would recommend indexing in the traditional way, rather than using the sublist approach.

0
source

All Articles