ArrayList initial capacity and IndexOutOfBoundsException [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

This question already has an answer here:

Consider this sample code:

List myList = new ArrayList(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(7), what is actually happening?

回答1:

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(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 createPrefilledList(int size, T item) {     ArrayList list = new ArrayList(size);     for (int i = 0; i 


回答2:

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  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.



回答3:

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 size())



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!