Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java.util.Vector的IndexOutOfBoundException_Java_Arrays_Dynamic_Vector_Indexoutofboundsexception - Fatal编程技术网

使用java.util.Vector的IndexOutOfBoundException

使用java.util.Vector的IndexOutOfBoundException,java,arrays,dynamic,vector,indexoutofboundsexception,Java,Arrays,Dynamic,Vector,Indexoutofboundsexception,我在使用java.util.Vector和java.util.ArrayList时遇到问题。 我知道容量,或者更好,向量中会保存多少元素。因此,我使用构造函数新向量(int-capacity)的实现初始化了java.util.List 在初始化列表之后,我使用了方法set(index,value),但是这个调用导致IndexOutOfBoundException。这非常令人困惑,因为我使用构造函数将容量设置为给定值 以下代码段显示了该问题: public void calculateSimp

我在使用
java.util.Vector
java.util.ArrayList时遇到问题。
我知道容量,或者更好,向量中会保存多少元素。因此,我使用构造函数
新向量(int-capacity)
的实现初始化了
java.util.List

在初始化列表之后,我使用了方法
set(index,value)
,但是这个调用导致
IndexOutOfBoundException
。这非常令人困惑,因为我使用构造函数将容量设置为给定值

以下代码段显示了该问题:

  public void calculateSimple(List<Stock> values, int n) {

    if (n<=0) {
        throw new IllegalArgumentException("N must not be zero or negativ");
    }       

    int max = values.size();

    result = new Vector<Double>(max);

    System.out.println("Size of result "+result.size());

    if (max == 0) {
        result.add(0.0);
    }

    if (max <= n) {
        n = max;
    }

    for (int i = 1; i <= n; i++) {          
        List<Double> subList = values.subList(max-i-n, max-i);
        result.set(result.size()-i, calculateSimpleValue(subList, n));
    }   
}
public void calculateSimple(列表值,int n){

如果(ncapacity
constructor参数并不意味着这样数量的元素将自动添加到
ArrayList
中。它只意味着将分配的内部缓冲区的初始大小。如果您可以提前估计列表中有多少元素以提高性能,则可以使用它。但是y您仍然需要将实际元素添加到列表中。您可以使用循环:

for(int i=0; i<max; i++) result.add(0.0);

我可以看到java.util.Vector.set()方法中的for(inti=0;i)

 /**
     * Replaces the element at the specified position in this Vector with the
     * specified element.
     *
     * @param index index of the element to replace
     * @param element element to be stored at the specified position
     * @return the element previously at the specified position
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
     *         ({@code index < 0 || index >= size()})
     * @since 1.2
     */
    public synchronized E set(int index, E element) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);

        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }
/**
*将此向量中指定位置处的元素替换为
*指定的元素。
*
*@param要替换的元素的索引
*@param-element要存储在指定位置的元素
*@返回之前指定位置的元素
*@如果索引超出范围,则引发ArrayIndexOutOfBoundsException
*({@code index<0 | | index>=size()})
*@自1.2
*/
公共同步E集(int索引,E元素){
如果(索引>=元素计数)
将新的ArrayIndex抛出BoundsException(索引);
E oldValue=元素数据(索引);
elementData[索引]=元素;
返回旧值;
}

因此,当您使用set方法时,
result.size()-i>=calculateSimpleValue(子列表,n)可能会
.Where
elementCount=elementdata.length
在实例化向量时在构造函数中。如果要创建以某些值(如0.0或null)初始化的列表,请使用以下快速方法:

ArrayList<Double> list = new ArrayList<>(Collections.nCopies(100, value));
ArrayList list=新的ArrayList(Collections.nCopies(100,value));

在上面的示例中,您使用子列表方法的点,如果max=n,那么在这种情况下,它将导致负值,这也会导致IndexOutOfBoundException

您现在在任何地方都没有使用Vector。Sry也尝试了ArrayList。更改了代码部分,您无法更新从未添加的值。在PM i我想容量问题是一个C++向量类的调整大小,但是这不是一个例子。好的。是的。但是我不想添加一些东西。我想在特殊的位置设置一些东西,而在最坏的情况下,给定的列表可以是空的,而不用简单的数组。你有一个空的列表。没有。所以你不能在任何位置设置任何东西,除非你之前在列表中添加了一些东西。就是这样。这很好。你在利用向量(集合)构造函数。Thx,不,它不能,因为如果n为0或负,我会抛出一个IllegalArgumentException。你的方程是max-i-n,你也设置了n=max,这意味着max-i-max=-i因此为负。不是吗???好吧,这是一个关键情况。我必须检查一下。但这不是解决我问题的答案。你因为0或0而获得了否决票否定的部分,但无论如何不能再投票了。所以我不允许,删除你的答案。我刚刚投票给你关于你的问题“Java抛出自定义错误”。Thx。根据set(int-index,E-element)的定义,它用于将向量中的现有元素替换为新元素。在您的情况下,由于您在开始时尚未初始化或填充向量,因此当您尝试进行设置时,它自然会给您一个IndexOutOfBoundException。