24

Consider this sample code:

List<String> myList = new ArrayList<String>(7);
myList.add(5, "Hello");
myList.removeAll(Collections.singleton(null));

System.out.println(myList.size() + " objects:" );
for (String s : myList) {
    System.out.println("\t" + s);
}

myList is initialized with an initial capacity of 7, then the next line attempts to add the String "Hello" at position 5. This throws an IndexOutOfBoundsException:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 0

I looked over this question about what "initial capacity" means in terms of an ArrayList. I understand that this particular constructor is allocating room for 7 String elements, and if we try to add 8 elements to the list it'll have to allocate more room.

What I don't understand is why it doesn't create an "empty" list of size 7, with null values at each index, similar to what would happen if we declared String[] myArray = new String[7]. I recall learning that ArrayList is Java's implementation of a dynamic array, so I'd expect a similar sort of behavior. If I don't actually have space for 7 Strings allocated when I declare new ArrayList<String>(7), what is actually happening?

Community
  • 1
  • 1
Roddy of the Frozen Peas
  • 11,100
  • 9
  • 37
  • 73
  • Capacity and size here are two entirely different things. You'd need to add 7 placeholder elements to do what you are attempting, not merely use an initial capacity of 7. `ArrayList` is not a substitute for an array. – obataku Aug 10 '12 at 19:32
  • 1
    See also http://stackoverflow.com/questions/8896758/initial-size-for-the-arraylist – Raedwald Feb 08 '16 at 15:24

3 Answers3

22

What I don't understand is why it doesn't create an "empty" list of size 7, with null values at each index, similar to what would happen if we declared String[] myArray = new String[7].

That would be useful in some cases... and not useful in others. Quite often you have an upper bound of the size of list you're going to create (or at least a guess) but then you populate it... and you don't want to have a list which then has the wrong size... so you'd have to maintain an index while you "set" values, and then set the size afterwards.

I recall learning that ArrayList is Java's implementation of a dynamic array, so I'd expect a similar sort of behavior.

No, it's really not. It's a list which can be resized and uses an array behind the scenes. Try not to think of it as an array.

If I don't actually have space for 7 Strings allocated when I declare new ArrayList<String>(7), what is actually happening?

You do have space for 7 string references. The buffer size (i.e. the capacity) is at least 7, but the logical size of the list is still 0 - you haven't added anything to it. It's like you've got a sheet of paper that's long enough for 7 lines, but you haven't written anything yet.

If you want a prefilled list, you can easily write a method to create one:

public static List<T> createPrefilledList(int size, T item) {
    ArrayList<T> list = new ArrayList<T>(size);
    for (int i = 0; i < size; i++) {
        list.add(item);
    }
    return list;
}
iota
  • 34,586
  • 7
  • 32
  • 51
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • If I look at source, Object[] elementDate = new Object[7];, doesn't that mean it should have 7 null elements based on default value prinicple (elementData is instance variable)? – kosa Aug 10 '12 at 19:34
  • @thinksteep: I'm not sure what point you're trying to make, or what source you're looking at. The *array* will have 7 (not 8) null values. But the *logical size* of the list is still 0. – Jon Skeet Aug 10 '12 at 19:34
  • Yes, 7 null values, and OP trying to insert element at index 5, which is valid location/index with null value, then why IndexOutofBoundException? My assumption is, null at index should be replaced with "Hello" – kosa Aug 10 '12 at 19:36
  • @JonSkeet: He's referring to the constructor code of `java.util.ArrayList`. – João Silva Aug 10 '12 at 19:37
  • @João: I figured that *might* be the case - but the original comment just stopped after the code part, without the question... – Jon Skeet Aug 10 '12 at 19:39
  • 1
    @thinksteep: No, it's *not* a valid location as far as the list is concerned. It's a valid *array* location, but it's greater than the size of the list (as reported by the `size()` method) which is 0 at that point, as no values have been added. You've got to differentiate between the capacity of the list and its logical size. – Jon Skeet Aug 10 '12 at 19:40
5

There is a difference between the initial capacity of an array, and its size (i.e., the number of elements your array contains). It's its size that is used to determine if you are trying to access an index that is out of bounds.

Here is the ArrayList.java method that does this check:

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

As you can see, it has nothing to do with the array's initial capacity. It's based solely on the number of elements it contains.

João Silva
  • 81,431
  • 26
  • 144
  • 151
2

The initial capacity only does one thing: it gives a suggestion (not a requirement) of how big the backing array should be. Logically, there is no difference between what operations are permitted with or without the suggestion, or what the suggestion is. The only changes will be in what internal operations might occur or not occur as an optimization suggestion.

You can only 'add' like that to positions that already exist in the array. The elements before position 5 don't exist yet, so it throws an exception. From the Javadoc:

Throws: IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

corsiKa
  • 76,904
  • 22
  • 148
  • 194