【问题标题】:insert element into Array index out of bounds java [duplicate]将元素插入数组索引超出范围java [重复]
【发布时间】:2012-09-27 22:21:55
【问题描述】:

我制作了一个名为 NumList 的 ADT,并在 NumArrayList 类中实现了它

在实现的方法中,有一个 insert(int i, double value),其中 value 被插入到 array[i] 中。

int numItems 是跟踪我的数组元素的计数器。

public void insert(int i, double value)
{
    if (numItems >= items.length)
    {
        double[] tempItems = new double [items.length * 2];
        for(int j =0 ; j < items.length; j++ )
        {
            tempItems[j] = items[j];

        }

        tempItems[items.length] = value;
        items = tempItems;

    }

    else
    {
        if (i > numItems)
        {
            items[numItems] = value;
        }

        else 
        {
            for (int k = i; k < numItems; k++)
            {
                items[k+1] = items[k];
            }

            items[i] = value;
        }
    }

    numItems++;
}

是我的方法,看起来很简单。

public static void main (String[] args)
{
    NumArrayList test;
    test = new NumArrayList();

    //System.out.println("this is how many initial items the initialized array has.");
    //System.out.println(test.items);
    test.insert(1, 0.1);
    System.out.println("have tried to insert value 0.1 @ position 1, that is the second element in array.");
    test.print();

是我的测试代码区,内置在同一个类中。

我收到一个错误,编译器声称我在第 47 行或在

有一个 ArrayIndexOutOfBoundsException
tempItems[items.length] = value;

我相信它试图告诉我我的项目初​​始化是错误的,

private double[] items;
private int numItems;


public NumArrayList()
{
    items = new double[0];
    numItems = 0;
}

但是初始化已经被比我优秀得多的程序员批准了,这些错误让我无处可去。或许我应该研究一下程序的哪一部分?

【问题讨论】:

    标签: java arrays exception insert


    【解决方案1】:

    您的初始化肯定是错误的。什么是合理的默认尺寸?对于 ArrayList,答案是 10。你可以随心所欲,但不能为零!如果将大小为 0 的数组的长度加倍,则新数组的长度仍为 0。

    int capacity; //stores the size of the array (items available)
    int numItems; //stores how many items are actually stored in the array.
    
    public NumArrayList()  {
        items = new double[10];
        numItems = 0;
        capacity = 10;
    }
    

    【讨论】:

    • hm 我想就是这样,我和我的导师都明白,由于我们在缺少数组大小的情况下通过复制/粘贴到新数组来更改数组的大小,所以初始化为 0 就可以了。 .失败。
    【解决方案2】:

    您必须记住,数组总是以索引 0 而不是 1 开始。因此,如果您的数组大小为 10,则最大索引为 9 而不是 10。

    tempItems[0] = first element;
    tempItems[1] = second element;
    

    等等等等

    假设您有 10 个元素,您的第十个元素将在 tempItems[9] 中。尝试访问 tempItems[10] 将引发您看到的异常。基本上,如果您正在寻找最后一个索引,您想要这样做:

    tempItems[items.length-1] = value;
    

    编辑:忘记这个。您在初始化时将数组索引加倍。请参阅上面 Thorn 的帖子。

    【讨论】:

    • 这是对的,但是在贴出的代码中,tempItems.length 等于 items.length*2,所以 tempItems[items.length] 并不是真正的问题。当然,除非构造函数指示这两个都为零。
    【解决方案3】:

    改一下

        tempItems[items.length] = value;
    

        tempItems[items.length-1] = value;
    

    数组索引从 0 开始,如果你的数组长度为 5,你的最后一个索引是 4

    【讨论】:

      【解决方案4】:

      一旦你为数组分配了位置,比如说items = new double[0];,那么你就不能改变数组的大小。如果数组初始化为 0,这意味着你有一个无用的数组。您添加的任何内容都会引发 Array index out of bounds 异常。

      要走的路是使用集合,特别是List 接口。

      List myList = new List(); //Create an empty list
      myList.add(item); //adds item to List
      

      还有其他 List 的实现,如ArrayListLinkedList 等...可以更好地满足您的需求。

      【讨论】:

        猜你喜欢
        • 2016-12-23
        • 2014-04-11
        • 1970-01-01
        • 1970-01-01
        • 2019-01-02
        • 2014-06-13
        • 1970-01-01
        • 1970-01-01
        • 2011-04-11
        相关资源
        最近更新 更多