An ArrayList in Java is a List that is backed by an array.
The get(index) method is a constant time, O(1), operation.
The code straight out of the Java library for ArrayList.get(index):
public E get(int index) {
RangeCheck(index);
return (E) elementData[index];
}
Basically, it just returns a value straight out of the backing array. (RangeCheck(index)) is also constant time)
An ArrayList in Java is a List that is backed by an array.
The get(index) method is a constant time, O(1), operation.
The code straight out of the Java library for ArrayList.get(index):
public E get(int index) {
RangeCheck(index);
return (E) elementData[index];
}
Basically, it just returns a value straight out of the backing array. (RangeCheck(index)) is also constant time)
It's implementation is done with an array and the get operation is O(1).
javadoc says:
The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.
arrays - Time Complexity for Java ArrayList - Stack Overflow
Why is the add(index, element) time complexity not constant in Java for array lists?
For array lists, you would have to move all elements to adjacent positions when you insert at an index. So it takes linear time ( more the number of elements already in the list, more time it takes to move them all ).
Also Java has nothing to do with time complexities of a data structure. It is universal.
More on reddit.comarraylist - Time complexity in Java - Stack Overflow
ArrayList Time Complexity (Big-O) for Insertion, Deletion, Retrieving and Checking a specific element
Without a schedule
If a simple add(element) takes constant time because we need to add to the end only, why does the other add(index, element) take linear time instead? I just started studying data structures and algorithms, so any advice would help.
For array lists, you would have to move all elements to adjacent positions when you insert at an index. So it takes linear time ( more the number of elements already in the list, more time it takes to move them all ).
Also Java has nothing to do with time complexities of a data structure. It is universal.
Because you can't just plop the element into the center of an array let's say. You put it in that place then have to shift all other elements to the right after it. For linkedlist where the actual insert is constant, finding the xth element would take linear time. Constant (adding to very end of list) is best case scenario bit linear is most realistic for it's use case.
This depends on where you're adding. E.g. if in an ArrayList you add to the front of the list, the implementation will have to shift all items every time, so adding n elements will run in quadratic time.
Similar for the linked list, the implementation in the JDK keeps a pointer to the head and the tail. If you keep appending to the tail, or prepending in front of the head, the operation will run in linear time for n elements. If you append at a different place, the implementation will have to search the linked list for the right place, which might give you worse runtime. Again, this depends on the insertion position; you'll get the worst time complexity if you're inserting in the middle of the list, as the maximum number of elements have to be traversed to find the insertion point.
The actual complexity depends on whether your insertion position is constant (e.g. always at the 10th position), or a function of the number of items in the list (or some arbitrary search on it). The first one will give you O(n) with a slightly worse constant factor, the latter O(n^2).
In most cases, ArrayList outperforms LinkedList on the add() method, as it's simply saving a pointer to an array and incrementing the counter.
If the woking array is not large enough, though, ArrayList grows the working array, allocating a new one and copying the content. That's slower than adding a new element to LinkedListโbut if you constantly add elements, that only happens O(log(N)) times.
When we talk about "amortized" complexity, we take an average time calculated for some reference task.
So, answering your question, it's not the same complexity: it's much faster (though still O(1)) in most cases, and much slower (O(N)) sometimes. What's better for you is better checked with a profiler.