I am iterating on the elements of a list of String objects one after the other:
LinkedList list;
// add values to the list here
for (int i = 0; i < list.size(); i++)
System.out.println(list.get(i));
Here, each and every time i invoke get() on list, the list is iterated from one of its ends all the way to the i-th element-- so the complexity of the above loop is O(n^2).
Is is a.) the same as above for enhanced-for loop, or b.) is for-loop maintaining the pointer where it's last have been and thus the complexity of the below loop is O(n)?
for (String s:list)
System.out.println(s);
If case (b) above -- which i think it is -- is there any advantage of using an iterator on the list. this is plain iteration-- there's no going back&forth. EDIT: ..and my list operation is read-only.
TIA.