开发者

for (Object object : list) [java] construction

开发者 https://www.devze.com 2022-12-30 12:34 出处:网络
My question, is, whether the sequence of elements picked from a list will always be the same, is this construction behaviour is deterministic for

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 th开发者_运维百科e 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?


Any descendant of List will provide an Iterator that iterates through the list in a deterministic proper order.

The for-each loop construct is compiled into code that uses the iterator() method which for a List is described in the following way in the api:

iterator

  public Iterator iterator()
Returns an iterator over the elements in this list in proper sequence.

Specified by:iterator in interface Collection

Returns:an iterator over the elements in this list in proper sequence.


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).


whether the sequence of elements picked from a list will always be the same

Yes. Quote from the API Javadoc:

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

2:

how it goes through list's elements, from 0 to size()-1

Yes.

3: No, you generally don't need to iterate through the list manually to get to an indexed element. The most used List implementation is ArrayList, which is random access, i.e. you can access any of its elements directly, in constant time. For LinkedList, list.get(i) does iterate through the list till the desired element implicitly, as this implementation is not random-access.


According to the API doc contract of List.iterator():

Returns an iterator over the elements in this list in proper sequence.

So yes, iteration over the elements of a List should happen from index 0 to size()-1. Theoretically, you could encounter an implementation that violates the contract, but then that's a bug in the implementation, not in your code.


Also, look at java.util.ListIterator.

List<Object> list = new ArrayList<Object>();
for(ListIterator<Object> it = list.listIterator(); it.hasNext(); )
{
  Object o = it.next();
  it.nextIndex();
  it.prevIndex(); // etc
}


for(Object o : list) makes implicit use of the List's Iterator, returning elements in a deterministic order. for(int i = 0; i < list.size(); i++) also selects items from the List in a deterministic order, as by definition, Lists are ordered.


@MichaelBorgwardt Such an implementation would be broken - it doesn't satisfy the contract for List.iterator.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号