Java超出范围错误

Java超出范围错误,java,data-structures,Java,Data Structures,为什么以下内容返回IndexOutOfBoundsException?(索引5,大小为0) gridList=newarraylist(9); 网格列表。添加(5,2); 我的印象是构造函数调用将我的arraylist初始化为 我对java非常陌生,所以很抱歉。它仍然是一个空列表(大小为1,唯一可用的索引是0)。只是容量是9。容量达到容量后,ArrayList将展开 注:尺寸和容量是两个不同的东西 数组列表为9,但列表为空。因此,您无法在位置5添加元素,因为列表中不存在该位置。请遵循Array

为什么以下内容返回IndexOutOfBoundsException?(索引5,大小为0)

gridList=newarraylist(9);
网格列表。添加(5,2);
我的印象是构造函数调用将我的arraylist初始化为

我对java非常陌生,所以很抱歉。

它仍然是一个空列表(大小为1,唯一可用的索引是0)。只是容量是9。容量达到容量后,
ArrayList
将展开


注:尺寸和容量是两个不同的东西

数组列表为9,但列表为空。因此,您无法在位置5添加元素,因为列表中不存在该位置。

请遵循ArrayList的源代码,您可以看到大小和容量是不同的概念。
checkBoundInclusive()
方法将
索引
大小
容量
进行比较

public ArrayList(int capacity)
   {
     // Must explicitly check, to get correct exception.
     if (capacity < 0)
       throw new IllegalArgumentException();
     data = (E[]) new Object[capacity];
   }


   public void add(int index, E e)
   {
      checkBoundInclusive(index);
      modCount++;
      if (size == data.length)
        ensureCapacity(size + 1);
     if (index != size)
        System.arraycopy(data, index, data, index + 1, size - index);
      data[index] = e;
      size++;
    }


    private void checkBoundInclusive(int index)
    {
      // Implementation note: we do not check for negative ranges here, since
      // use of a negative index will cause an ArrayIndexOutOfBoundsException,
      // a subclass of the required exception, with no effort on our part.
      if (index > size)
        throw new IndexOutOfBoundsException("Index: " + index + ", Size: "
                                            + size);
    }
公共阵列列表(内部容量)
{
//必须显式检查,才能获得正确的异常。
如果(容量<0)
抛出新的IllegalArgumentException();
数据=(E[])新对象[容量];
}
公共无效添加(整数索引,E)
{
检查边界(索引);
modCount++;
if(size==data.length)
保证资本(规模+1);
如果(索引!=大小)
数组副本(数据,索引,数据,索引+1,大小-索引);
数据[指数]=e;
大小++;
}
私有void checkBoundInclusive(整数索引)
{
//实现说明:这里不检查负范围,因为
//使用负索引将导致ArrayIndexOutOfBoundsException,
//所需异常的一个子类,我们不做任何努力。
如果(索引>大小)
抛出新IndexOutOfBoundsException(“索引:+Index+”,大小:
+尺寸);
}

调用该构造函数只指定初始容量,但对ArrayList的大小没有影响(在添加任何内容之前,大小始终为零)。这在中进行了解释,并通过打印ArrayList证明:

ArrayList<Integer> gridList = new ArrayList<Integer>(9);
System.out.println(gridList);

Output: []
ArrayList gridList=新的ArrayList(9);
System.out.println(网格列表);
输出:[]
如果要使用9个整数(例如,9个零)初始化ArrayList,请尝试此“”:

arraylistgridlist=newarraylist(Collections.nCopies(9,0));
System.out.println(网格列表);
输出:[0,0,0,0,0,0,0,0,0,0,0]

如您所见,在初始化过程中,这会用值填充ArrayList,因此您现在可以调用
gridList.add(5,2)没有
索引自动边界异常

,正如其他人所指出的,在这种情况下,您将在位置5处输入元素,但您在那里什么都没有,之前也没有元素-请看这里:

    ArrayList<Integer>  g = new ArrayList<Integer>(9);
    g.add(0, 10);
    g.add(1, 20);
    g.add(2, 30);
    g.add(3, 40);

    for(Integer v: g) System.out.print(v + " ");
    System.out.println();

    g.add(2,99);

    for(Integer v: g) System.out.print(v + " ");
    System.out.println();

    g.add(88); // use this to just push value at the end

    for(Integer v: g) System.out.print(v + " ");
    System.out.println();

如何快速有效地使用0值将arraylist初始化为该容量?如果您的意思是使用大于0的大小进行初始化,则不能进行初始化,除非您将其初始化为另一个列表,否则它不会为空。您必须在大小增加之前添加值。如果要使用索引,请尝试使用数组而不是arraylist。然后,如果您想将其用作ArrayList,您可以使用
Arrays.asList(yourArray)
如果您已经有一个设置大小(9),则数组将是有效的。我将使用数组,但它没有“contains”方法。不幸的是,它没有一个更健壮的API。
if(Arrays.asList(yourray).contains(something)){}
您给出的数字是容量而不是大小。容量是指在不调整基础数据结构的情况下可以添加多少元素,即您希望列表中有多少元素,而不是列表中有多少元素。
ArrayList<Integer> gridList = new ArrayList<Integer>(Collections.nCopies(9, 0));
System.out.println(gridList);

Output: [0, 0, 0, 0, 0, 0, 0, 0, 0]
    ArrayList<Integer>  g = new ArrayList<Integer>(9);
    g.add(0, 10);
    g.add(1, 20);
    g.add(2, 30);
    g.add(3, 40);

    for(Integer v: g) System.out.print(v + " ");
    System.out.println();

    g.add(2,99);

    for(Integer v: g) System.out.print(v + " ");
    System.out.println();

    g.add(88); // use this to just push value at the end

    for(Integer v: g) System.out.print(v + " ");
    System.out.println();
10 20 30 40 
10 20 99 30 40 
10 20 99 30 40 88