显示java.lang.ArrayIndexOutOfBoundsException:在某些测试用例中,索引2超出长度2



代码:

static void exchangeColumns(int matrix[][])
{    
int i;
int n = matrix[0].length;
for (i=0;i<n;i++){

int temp = matrix[i][0];
matrix[i][0] = matrix[i][n-1];
matrix[i][n-1] = temp;

}
}

您使用了错误的方法来迭代多维数组。请使用以下方法对数组进行迭代。

for (int i = 0; i < matrix.length; ++i) {
for(int j = 0; j < matrix[i].length; ++j) {
System.out.println(matrix[i][j]); // Here you can place your logic by accessing the array elements
}
}

最新更新