java.lang.ArrayIndexOutOfBoundsException:错误

时间:2015-04-02 10:36:17

标签: java arrays arraylist

我有一些数组,我想删除所有元素都等于零的数组。

  

0 [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
  1 [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
  2 [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
  3 [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
  4 [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
  5 [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
  6 [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
  7 [0.152,0.253,0.102,0.304,0.355,0.355,0.152,0.203,0.203]
  8 [0.135,0.135,0.135,0.325,0.325,0.325,0.189,0.189,0.189]
  9 [0.302,0.302,0.302,0.755,0.755,0.755,0.453,0.453,0.453]
  10 [0.209,0.209,0.209,0.546,0.546,0.546,0.337,0.337,0.337]
  11 [0.339,0.527,0.433,0.79升0.904,0.941,0.49,0.584,0.584]
  12 [0.262,0.262,0.262,0.669,0.669,0.669,0.407,0.407,0.407]
  13 [0.054,0.054,0.054,0.162,0.162,0.162,0.108,0.108,0.108]
  14 [0.19,0.228,0.19,0.418,0.456,0.418,0.266,0.342,0.266]
  15 [0.067,0.067,0.067,0.132,0.132,0.132,0.067,0.067,0.067]
  16 [0.321,0.581,0.441,0.681,0.842,0.882,0.421,0.541,0.541]
  17 [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]

我尝试用Array列表编写代码,用2D-Array编写代码,但我总是会出错。

当我尝试使用ArrayList;

时此代码
int binningNumber = 19;
int numberOfPixels = angle.getHeight() * angle.getWidth();

ArrayList<float[]> allVectors = new ArrayList<float[]>();

for(int i=0;i<angle.getHeight();i++)
{
    for(int j=0;j<angle.getWidth();j++)
    {
        float angleVal = angle.get(i, j);
        float magnitudeVal = magnitude.get(i, j);
        float[] input2 = new float[19];
        input2 = zeroVectorBinning(angleVal,magnitudeVal,binningNumber);
        allVectors.add(input2);
    }
}
for (int i = 0; i < allVectors.size(); i++) {
    if (isZeros(allVectors.get(i))) {
        allVectors.remove(i);
    }
}


/*****************Remove Zero elements Method ****************/
private static boolean isZeros(float[] arr) {
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] != 0) {
            return false;
        }
    }
    return true;
} 

这些都是错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at boofcv.ExampleInterestPoint.preSmoothing(ExampleInterestPoint.java:316)
    at boofcv.ExampleInterestPoint.getInerestPointsChunks(ExampleInterestPoint.java:173)
    at boofcv.ExampleInterestPoint.main(ExampleInterestPoint.java:668)

当我尝试使用2D阵列时这段代码

int numberOfPixels = angle.getHeight() * angle.getWidth();
int binningNumber = 19;

float[][] allVectors = new float [numberOfPixels][binningNumber];
float[][] allVectorsOfZeros = new float [numberOfPixels][binningNumber];
int index = 0;
float[] input2 = new float[19];
for(int i=0;i<angle.getHeight();i++)
{
    for(int j=0;j<angle.getWidth();j++)
    {
        float angleVal = angle.get(i, j);
        float magnitudeVal = magnitude.get(i, j);                       
        input2 = zeroVectorBinning(angleVal,magnitudeVal,binningNumber);
        for (int i1 = 0; i1 < input2.length; i1++) {
            if (input2[i1] != 0) {
                allVectors[index++] =input2;
            }
            else
            {
                allVectorsOfZeros[index++] =input2;
            }

        }
    }
}

它总是给我这些错误,

我有两个2D阵列,一个用于阵列,所有元素都等于零 和另一个有值的数组。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
    at boofcv.ExampleInterestPoint.preSmoothing(ExampleInterestPoint.java:297)
    at boofcv.ExampleInterestPoint.getInerestPointsChunks(ExampleInterestPoint.java:170
    at boofcv.ExampleInterestPoint.main(ExampleInterestPoint.java:660)

我该如何解决?

2 个答案:

答案 0 :(得分:0)

问题是你在问一些不存在的数组索引......用你提供的代码无法弄清楚它是什么......

为了提出您的问题,我已经做了一个检查数组中是否只有零的方法... is the demo

private static final Float ZERO = 0f;

private static void checkIfZeros(float[] simpleArray) {
    boolean moreThanZeros = false;

    for (int i = 0; i < simpleArray.length; i++) {
        // check if there are only zeros
        if (!Float.valueOf(simpleArray[i]).equals(Float.valueOf(ZERO)))
        {
            moreThanZeros = true;
            break;
        }

    }

    if(moreThanZeros)
        System.out.println("ARRAY WITH MORE THAN ZEROS");
    else
        System.out.println("CANDIDATE TO DELETE");

}

方法为static,现在返回void用于演示广告,但我建议将其声明为:

private boolean checkIfZeros(float[] simpleArray)

如果删除它,请在内部调用它:

if (!checkIfZeros(array))
    // delete

注意:这适用于一维数组,如果您需要在现有数组中添加新的for-loop以检查2D ...如果需要,请询问更多信息。

答案 1 :(得分:0)

我认为两种方法中的问题并不相同,即使它们在两种情况下都是不合格的。

从第二种方法开始,检查零的最内层循环中的逻辑是错误的。对于index的每个测试,allVectorsinput2[i1]的第一个维度)会递增,这取决于此昏暗的大小将在某个点溢出。建议的逻辑是

    for(int i=0;i<angle.getHeight();i++) {
        for(int j=0;j<angle.getWidth();j++) {       
            //...

            boolean allZeros = true;
            for (int i1 = 0; allZeros && i1 < input2.length; i1++) {
                allZeros = (input2[i1] == 0);
            }
            // increment index only once input2 is tested
            if (allZeros) {
                allVectorsOfZeros[index++] = input2;
            }
            else
            {
                allVectors[index++] = input2;
            }
         }
      }

对于第一种方法,我没有发现实际的ArrayIndexOutOfBound,但代码不会按预期执行。纠正它可以解决错误。

问题来自于从列表中删除向量的方式。在这个循环中:

for (int i = 0; i < allVectors.size(); i++) {
   if (isZeros(allVectors.get(i))) {
       allVectors.remove(i);     // <= pb here
   }
}

如果您阅读public E remove(int index)

的de javadoc
  

删除此列表中指定位置的元素。将任何后续元素向左移位(从索引中减去一个)。

由于您从0循环到allVectors.size(),并假设您有4个向量,全部为零,您将删除

  • 索引0处的向量,然后元素在左侧移位,大小现在为3
  • 索引为1的向量,大小现为2。

你将有效地删除原始向量中的奇数索引(在我上面的例子中)并留下2个向量的零,但没有删除。解决方案是从列表末尾开始删除,然后没有元素转换,或者使用ListIterator#remove()的迭代器(没有索引)。


编辑 - 请注意:

float[][] allVectors = new float [numberOfPixels][binningNumber];
float[][] allVectorsOfZeros = new float [numberOfPixels][binningNumber];

你分配一个(numberOfPixels * binningNumber)2d数组。进一步的时候

allVectors[index++] =input2;

不使用为第二维分配的数组。没有input2[]allVectors[index][]的元素副本。并且没有控件来查看input2的大小是否与初始大小匹配。 发生的事情只是allVectors[index]将引用input2。例如,即使未使用foo的声明的2d维度

,以下代码仍然有效
   int [][] foo = new int[2][3];
   foo[0] = new int[] {1,2,3,4,5};
   foo[1] = new int[] {1,2,3};

通常,您可以将2d dim声明为[],未指定长度的数组。第一个暗淡的每个元素都包含null,例如

   int [][] foo = new int[2][];
   // foo[0][0] = 42; => NullPointException

   int [][] foo = new int[2][0];
   // foo[0][0] = 42; => ArrayOutOfBoundException
   // but foo[0].length == 0

这种最后一种方式可能是首选,因为您在循环时不需要测试null,例如

for (int i = 0; i < foo.length; i++) {
   for (int j = 0; j < foo[i].length; j++) { 
      //...
   }
}