My question, is, whether the sequence of elements picked from a list will always be the same,
is this construction behaviour is deterministic for
java "List"s - descendants of java.util.List
2) question, if I use
for(Object o: list)
construction and inside the loop’s body increment a variable,
will it be the index of list’s elements? So, how it goes through list’s elements, from 0 to size()-1 or chaotically?
List.get(i)
will always return this element?
3) question ( I suppose for the 2-nd question the answer will be negative, so:)
for (int i=0; i < list.size(); i++) {
}
is the best way if I need to save the index of an element and later get it back from a list by its id?
Overall:
I simply need to save a position in the list in every object’s field.
object.setPosInList(currentIndexOfTheLoop)
Now clear?
1) Yes
2) Yes
3) No.
Also note that on LinkedLists using
get(i)is O(i) while getting the next element from an iterator (which is what for-each does) is O(1).