0

I have created an ArrayList with initial capacity. When I try to insert an element other than 0th index, I cannot pass ArrayList.rangeCheckForAdd method.

My question is even I specify the intial capacity of ArrayList(As I know It creates an empty array with this capacity), Why there is a range check?

ArrayList<String> list = new ArrayList<>(100);

list.add(1, "test");
hellzone
  • 5,393
  • 25
  • 82
  • 148
  • 3
    "I specify the intial capacity" - that's the point: you're defining the _capacity_ while the size of the list is still 0. So you're trying to add an element at an index that doesn't exist. Note that lists are not arrays (even though ArrayList internally uses one) so the size can change and valid indices to add elements are in the range `[0,size]`. – Thomas Jul 12 '21 at 15:52
  • To go along with what Thomas said, if you print the `size` with `System.out.println(list.size());`, it will be `0`, not `100`. – Nexevis Jul 12 '21 at 15:53
  • @Thomas Actually what I don't get is why size of the list is matter? What is the difference between new String[100] and new ArrayList(100) when I try to add a new element? – hellzone Jul 12 '21 at 15:56
  • `new String[100]` creates an array of fixed length and with all elements being `null` initially. `new ArrayList(100)` creates a list of variable length which _internally_ creates an array of length 100 but that array will be replaced later on. Semantically `ArrayList` and `LinkedList` are the same and you can't add an element to index 1 of an empty linked list. You might want to revisit the difference between arrays and lists - and note that `ArrayList` is just a specific implementation of `List`. – Thomas Jul 12 '21 at 16:03

3 Answers3

3

The capacity of an ArrayList is the number of elements in the array used to store list elements.

The size of an ArrayList is the number of elements in the list.

You can only add() elements at indexes ranging from zero to size, so if the current size is zero, you can only specify an index of zero.

However, the array that backs the list will not have to be replaced until the size exceeds the original capacity.


Collections objects enforce contracts to maintain invariants for the state of the collection. In this case, lists ensure that an element at an index can be read only if it was explicitly added to the list. If you could skip index 0 and add an element at index 1, what should happen if you get index 0? Return null? Throw NoSuchElementException? A primary difference between an ArrayList and an Object[] is that the list constrains use of the array to provide well-defined behavior.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • When I create an ArrayList with initial capacity, It creates an array behind the scenes. What I don't get is I can insert element to an array with specific index without any problem. But Why I cannot insert an element to this array-backed ArrayList? – hellzone Jul 12 '21 at 16:02
  • @hellzone I updated my answer to address that. – erickson Jul 12 '21 at 16:23
2

The parameter with value 100 is called initialCapacity and for internal purpose. That does not mean that your list has 100 entries.

You are able to use the next unused index. And because index begins for java at 0, you have use 0.

  ArrayList<String> list = new ArrayList<>(100);

  list.add(0, "test");
Michael Katt
  • 615
  • 1
  • 5
  • 18
-1

your code is kind like this.

ArrayList<String> list = new ArrayList<>(100);

[][]......[][] -> length 100

list.add(1, "test");

[][test][].....[][] -> length 101

so if u wanna change value of index 1. u need to use

list.set(1,"test");

or u can just declare without length limitation.

ArrayList<String> list = new ArrayList<>();

then u can use add method.

Cerakoted
  • 19
  • 4
  • `new ArrayList<>(100);` still creates a list of size (aka length) 0. There's a difference between capacity and size which you might want to look up. – Thomas Jul 12 '21 at 16:05