java - Showing java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2 in some test cases?
问题描述
Code:
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
}
}
推荐阅读
- angular - 如何创建 Angular Universal Dockerfile 和 docker-compose.yml 文件?
- bash - 使用命令行将多个 CSV 从本地源上传到 GBQ
- parquet - Azure 数据工厂中复制活动的基于规则的映射
- artificial-intelligence - 在 alpha beta 剪枝中使用启发式函数
- r - 如何使用 spread() 使一列的值成为主列名
- sql - 使用 SQL,如何提高仅在两列中相互出现的对的计数频率
- c# - 参考管理器对话框中缺少装配选项卡
- c++ - 如何对使用基类构造函数的派生类进行参数推导?
- python - Python 使用单个打印命令打印每个嵌套列表的第一项
- here-api - HERE 地图 - 路线时间不正确