I am wondering if we can use index to access List
For example:
List list;
list[5] //blah....
There are plenty of good answers here, but I just want to point out that list.get(i)
is the same as list[i]
only if list is implemented with an array (i.e ArrayList). If it is a LinkedList you are not really indexing with get
, but rather iterating.
So if you use get()
with the interface type List, especially in a loop, you should check how it's implemented, as get()
with ArrayList is O(1) whereas get()
with LinkedList is O(n) (much slower).