ArrayList 初始容量和 IndexOutOfBoundsException [英] ArrayList initial capacity and IndexOutOfBoundsException

查看:33
本文介绍了ArrayList 初始容量和 IndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个示例代码:

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("	" + s);
}

myList 初始化为 7 的初始容量,然后下一行尝试在位置 5 添加字符串Hello".这将引发 IndexOutOfBoundsException:

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:

线程main"中的异常java.lang.IndexOutOfBoundsException:索引:5,大小:0

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

我查看了这个问题关于初始容量"在 ArrayList 方面的含义.我知道这个特定的构造函数正在为 7 个 String 元素分配空间,如果我们尝试将 8 个元素添加到列表中,它将不得不分配更多空间.

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.

理解的是为什么它不创建一个大小为 7 的空"列表,每个索引都有空值,类似于我们声明 时会发生的情况String[] myArray = new String[7].我记得了解到 ArrayList 是 Java 对动态数组的实现,所以我希望有类似的行为.如果我在声明 new ArrayList(7) 时实际上没有分配 7 个字符串的空间,实际发生了什么?

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?

推荐答案

我不明白的是为什么它不创建大小为 7 的空"列表,每个索引处都有空值,类似于如果我们声明 String[] myArray = new String[7] 会发生什么.

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

这在某些情况下很有用……而在其他情况下则没有用.很多时候,你有一个上限你要创建的列表大小(或者至少是一个猜测),但是你填充它......而你没有em> 想要一个大小不正确的列表......所以你必须在设置"值时维护一个索引,然后再设置大小.

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.

我记得了解到 ArrayList 是 Java 对动态数组的实现,所以我希望有类似的行为.

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.

如果我在声明新的 ArrayList(7) 时实际上没有分配 7 个字符串的空间,那么实际发生了什么?

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

确实有 7 个字符串引用的空间.buffer 大小(即容量)至少为 7,但列表的 逻辑 大小仍然为 0 - 您尚未向其中添加任何内容.就好像你有一张纸,足够长 7 行,但你还没有写任何东西.

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;
}

这篇关于ArrayList 初始容量和 IndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆