I know arrays are faster at getting and setting, while LinkedLists are better at adding and removing elements, but what about when iterating? A more "traditional" for(i=0;i<intList.size();i++)
would definitely make LinkedLists slower since you'd have to get the element at index i every time. But what if I use for(int i : intList)
? How does it work under the hood for this instance? For example:
LinkedList<Integer> intList = new LinkedList();
/*
populate list...
*/
for (int i : intList) {
//do stuff
}
I imagine that there's no need to get an specific element when going through the whole List, so it should be possible to have some sort of loop implementation where performance is about the same. Though I don't know how exactly how for
is implemented for this example so I can't be sure if this is it.