0

I understand that size and capacity are different things. My question is why can I insert at a random location in a newly created array, but cannot do so in a newly created ArrayList. And I understand how it works, I'm interested in the motivation for doing it this way in the source code.

I faced the problem that I can't add value to ArrayList after initialisation with index.

final ArrayList<Integer> a = new ArrayList<>(16);
a.add(9, 1);

This code will throw IndexOutOfBoundsException, this happens because of this method in ArrayList source code.

public void add(int index, E element) {
  rangeCheckForAdd(index);
  ...
}

    private void rangeCheckForAdd(int index) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

My question is why this restriction exists? As far as I know, when I init ArrayList inside of it allocated simple java array of Object with default capacity = 10 or another capacity, which I can provide to constructor.

Can anyone explain me what benefits we can get from that? Thanks a lot.

  • 2
    Does this answer your question? [Why start an ArrayList with an initial capacity?](https://stackoverflow.com/questions/15430247/why-start-an-arraylist-with-an-initial-capacity) – OH GOD SPIDERS Jan 20 '22 at 16:17
  • 3
    Note that "capacity" and "size" are not the same. – OH GOD SPIDERS Jan 20 '22 at 16:18
  • 1
    If the first item in a list is at index 9, what will the elements with lower indices be? – Scott Hunter Jan 20 '22 at 16:20
  • I understand that size and capacity are different things. My question is why can I insert at a random location in a newly created array, but cannot do so in a newly created ArrayList. – Stepan Borisov Jan 20 '22 at 16:23
  • The short answer is: Because Lists and the `add(int index, E element)` are designed like that. Arrays might get initialized with default values (eG null for Objects). But not every implementation of the List interface is backed by an Array in the background. You have LinkedList for example that has linked Nodes as background instead of an Array. .... – OH GOD SPIDERS Jan 20 '22 at 16:32
  • ... Sure you could have designed the `add(int index, E element)` in a way that if you were to insert something at index 1000 in an empty LinkedList it would automatically create 999 Nodes with the default value and insert those first, but it was decided that this is a less intuitive and useful than designing that method and Lists in a way that no automatic insertion of elements happens in the background. – OH GOD SPIDERS Jan 20 '22 at 16:33
  • No one of the normal users here on Stackoverflow has had any part in designing the Java Language so I like most people can only speculate on the "WHY" part. Beside that I already gave you my opinion on why that restriction exists: The add method you are talking about is **not** exclusive to `ArrayList`. It is a part of the general `java.util.List` interface and hence designing it in a way that would make sense for `ArrayList` but cause possible troubles for any List that is not backed by an Array in the background (like `LinkedList`) makes no sense to me. – OH GOD SPIDERS Jan 20 '22 at 16:43
  • 1
    You can not “insert at a random location in a newly created array”, as arrays do not support insertion at all. You can only *set* an element of an array, at an index smaller than the array’s size. Just like with a `List`. – Holger Jan 20 '22 at 17:44

0 Answers0