Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ArrayList#rangeCheck not check if the index is negative?

Tags:

java

arraylist

ArrayList#get, set and remove call the rangeCheck method at first. This method does not check if the index is negative. It only checks if the index is greater than or equal to the length of the array. Javadoc explains the reason; an array access throws an ArrayIndexOutOfBoundsException if index is negative.

private void rangeCheck(int index) {
    if (index >= size)
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
public E get(int index) {
    rangeCheck(index);

    return elementData(index);
}

According to Java Langauage Specification, an array access throws an ArrayIndexOutOfBoundsException if index is not only negative but also gte.

All array accesses are checked at run time; an attempt to use an index that is less than zero or greater than or equal to the length of the array causes an ArrayIndexOutOfBoundsException to be thrown.

I think that rangeCheck should check both negative and gte or, for performance, should check neither. Why does not rangeCheck check if the index is negative?

like image 744
kshi Avatar asked Aug 15 '16 06:08

kshi


People also ask

Why would you use an ArrayList?

ArrayList in Java is used to store dynamically sized collection of elements. Contrary to Arrays that are fixed in size, an ArrayList grows its size automatically when new elements are added to it. ArrayList is part of Java's collection framework and implements Java's List interface.

Why are Arraylists better than arrays?

An Array is a collection of similar items. Whereas ArrayList can hold item of different types. An array is faster and that is because ArrayList uses a fixed amount of array. However when you add an element to the ArrayList and it overflows.

Why ArrayList is not good for manipulation?

Manipulating ArrayList takes more time due to the internal implementation. Whenever we remove an element, internally, the array is traversed and the memory bits are shifted. Manipulating LinkedList takes less time compared to ArrayList because, in a doubly-linked list, there is no concept of shifting the memory bits.

Does ArrayList extend List?

The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.


1 Answers

Very simply because in an ArrayList the backing array may be larger than the current size.

In the current implementation, an ArrayList's backing array capacity increases by a factor of 1.5 each time the current maximum capacity is exceeded. The default initial capacity is 10, and when you try to add the 11th element to the list the array gets reallocated with a capacity of 15. When you exceed 15 it goes to 22, etc.

At any given time, the capacity is likely larger than the current number of elements in the ArrayList.

Checking for negative indexes is left to the JVM (on the backing array) and ArrayList itself needs to check only at the upper end of the current list.

like image 198
Jim Garrison Avatar answered Oct 16 '22 23:10

Jim Garrison