2017-02-12 91 views
-1

我试图从double[][] 3x3矩阵(计算行列式)获取子数组。我不断收到ArrayIndexOutOfBoundsException从2D double [] []数组获取子数组

任何想法,为什么?

public double[][] get2DSubArray(double[][] largeArray, int rowStartIndex, int rowEndIndex, int columnStartIndex, int columnEndIndex) { 
    double[][] subArray = new double[rowEndIndex-rowStartIndex+1][columnEndIndex-columnStartIndex+1]; 
    for (int row = rowStartIndex; row < rowEndIndex; row++) { 
     subArray[row] = Arrays.copyOfRange(largeArray[row], columnStartIndex, columnEndIndex); 
    } 
    return subArray; 
} 
+0

你知道哪些索引值导致异常吗? –

+0

当我输入一个3x3矩阵时,发生异常(索引为1,2,1,2)(例如rowStartIndex = 1,rowEndIndex = 2等等) –

+0

我已经用上面的输入更新了答案。 –

回答

1

看起来它有事情做与数组初始化,传递给方法的array似乎并不为3×3。 E.g,以下不产生异常:

public static void main(String[] args) throws IOException { 
    double[][] array = new double[][]{{1d,1d,1d},{2d,2d,2d},{3d,3d,3d}}; 
    double[][] subArray = get2DSubArray(array, 1, 2, 1, 2); 
    for(double[] arrayElement : subArray){ 
     for(double number : arrayElement){ 
      System.out.println(number); 
     } 
    } 
} 

public static double[][] get2DSubArray(double[][] largeArray, int rowStartIndex, int rowEndIndex, int columnStartIndex, 
     int columnEndIndex) { 
    double[][] subArray = new double[rowEndIndex - rowStartIndex + 1][columnEndIndex - columnStartIndex + 1]; 
    for (int row = rowStartIndex; row < rowEndIndex; row++) { 
     subArray[row] = Arrays.copyOfRange(largeArray[row], columnStartIndex, columnEndIndex); 
    } 
    return subArray; 
} 

更新

虽然上述溶液中不产生异常,也不会产生正确的输出也是如此。主要是因为以下几个原因:

  • Arrays.copyOfRange方法第三个参数是排他性的,因此我们必须通过columnEndIndex+1它的工作
  • 对于仅环所提供的参数集,而应该执行执行至少一次两次
  • 不是指定Arrays.copyOfRangesubArray[row]的,我们需要把它下面的解决方案不工作分配给subArray[<zero based index>]

public double[][] get2DSubArray(double[][] largeArray, int rowStartIndex, int rowEndIndex, int columnStartIndex, 
     int columnEndIndex) { 
    double[][] subArray = new double[rowEndIndex - rowStartIndex + 1][columnEndIndex - columnStartIndex + 1]; 
    int index = 0; 
    for (int row = rowStartIndex; row <= rowEndIndex; row++) { 
     subArray[index++] = Arrays.copyOfRange(largeArray[row], columnStartIndex, columnEndIndex+1); 
    } 
    return subArray; 
} 
+0

如果你减去rowStartIndex而不是创建一个新变量会更高效。 – Locke

+0

谢谢。我加了1到columnEndIndex,使用'row <= rowEndIndex'而不是'row

1

如果该行开始为500和年底是505,在for循环的变量将在500而不是0要替换启动“子数组[行] =”与“子阵[按行rowStartIndex] =“。您正在引用较大阵列中的位置,与复制位于较小阵列中的位置相比。

编辑:

//修正版本:

public static double[][] get2DSubArray(double[][] largeArray, int rowStartIndex, int rowEndIndex, int columnStartIndex, 
      int columnEndIndex) { 
     double[][] subArray = new double[rowEndIndex - rowStartIndex + 1][columnEndIndex - columnStartIndex + 1]; 
     for (int row = rowStartIndex; row <= rowEndIndex; row++) { 
      subArray[row-rowStartIndex] = Arrays.copyOfRange(largeArray[row], columnStartIndex, columnEndIndex+1); 
     } 
     return subArray; 
    } 
+0

我只是改变了,但仍然得到例外。看来Eclipse并不了解我想制作一个实际的矩阵(二维数组),因为它无法识别我的二维数组中有第二列。这里是我的确切的输出(我打印矩阵只是为了确保它的工作原理)http://imgur.com/a/lmEDF –