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

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

问题描述

考虑这个示例code:

 列表<串GT; myList中=新的ArrayList<串GT;(7);
myList.add(5,你好);
myList.removeAll(Collections.singleton(NULL));的System.out.println(myList.size()+对象:);
对于(一个String:myList中){
    的System.out.println(\\ t+ S);
}

myList中与7的初始容量初始化,则下一行尝试添加字符串Hello在位置5。这将引发IndexOutOfBoundsException异常:


  

异常线程mainjava.lang.IndexOutOfBoundsException:指数:5,大小:0


我看了看<一个href=\"http://stackoverflow.com/questions/4172480/whats-meant-by-parameter-int-initial-capacity-in-an-arraylist\">this问题什么初始容量,在一个ArrayList的术语表示。据我所知,这种特殊构造7串要素分配的房间,如果我们尝试8种元素添加到列表中,它会不得不分配更多的空间。

我的有什么不的理解就是为什么它不产生规模7的空的列表,每个指标在空值,类似于会发生什么,如果我们声明的String [] myArray的=新的String [7] 。我记得当得知ArrayList是Java的实现一个动态数组,所以我期望类似的那种行为。如果我实际上没有因为当我宣布新的ArrayList&LT分配7串空间;串&GT;(7),什么是真正发生的事情


解决方案

  

我不明白的是,为什么它不创建规模7的空的列表,每个指标在空值,类似于会发生什么,如果我们宣布的String [] myArray的=新的String [7]


这将是在某些情况下有用...,而在其他没有用处。很多时候,你有一个的上限的你要创建(或至少是猜测)列表的大小,但你填充它...你的希望有一个列表,然后有错误的大小...所以你必须要保持一个索引,而你设置值,然后设置大小之后。


  

我记得当得知ArrayList是Java的实现一个动态数组,所以我期望类似的那种行为。


没有,这真的不是。它是可以调整大小的使用的一个幕后数组列表。尽量不要把它看成一个数组。


  

如果我实际上并不具有分配的7弦空间时,我宣布新的的ArrayList&LT;弦乐&GT;(7),什么是真正发生的事情


您的的有七弦的参考空间。该的缓存的大小(即容量)为至少7个,但是逻辑的列表的大小仍然是0 - 你还没有添加任何东西给它。这就像你有一张纸这是足够长的7条线,但是你有没有写任何东西。

如果您的希望的一个prefilled列表,你可以很容易地编写一个方法来创建一个:

 公共静态列表&LT; T&GT;创建prefilledList(INT大小,T项){
    ArrayList的&LT; T&GT;名单=新的ArrayList&LT; T&GT;(大小);
    的for(int i = 0; I&LT;大小;我++){
        list.add(项目);
    }
}

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?

解决方案

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

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

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