Why would iterating over a List be faster than indexing through it?

后端 未结 5 1695
小蘑菇
小蘑菇 2020-12-12 11:38

Reading the Java documentation for the ADT List it says:

The List interface provides four methods for positional (indexed) access to list elements. Li

5条回答
  •  爱一瞬间的悲伤
    2020-12-12 12:01

    To find the i-th element of a LinkedList the implementation goes through all elements up to the i-th.

    So

    for(int i = 0; i < list.length ; i++ ) {
        Object something = list.get(i); //Slow for LinkedList
    }
    

提交回复
热议问题