我有兴趣通过将单和双ArrayLists的非null参数传递给类方法来返回int [] [],例如:
//outer loop
ArrayList<ArrayList<Integer>> rowsInMatrixA;
//inner loop
ArrayList<Integer> colsInMatrixA;
仅使用本机Java。我在这里搜索过高低,在Java API,Java教程和许多其他地方。欢迎所有的想法和建议。
编辑:这是我尝试的内容:
public static int[][] convertIntegers(ArrayList<ArrayList<Integer>> rows, ArrayList<Integer> cols)
{
int[][] newMatrix = new int[rowsInMatrixA.size()][colsInMatrixB.size()];
//iterate outer, through rows
for (int i=0; i < newMatrix.length; i++)
{
//iterate inner, through columns
for(int j = 0; j < newMatrix[0].length; j++){
newMatrix[i][j] = rows.get(i).get(j);
}
}
return newMatrix;
}
它仍然不起作用:当我编译并尝试在main中获取打印输出时,我在此行上得到一个IndexOutOfBoundsException:
newMatrix[i][j] = rows.get(i).get(j);
如何解决这个问题?我将不胜感激任何帮助。感谢。
答案 0 :(得分:0)
我认为这可以通过循环遍历arraylist轻松完成。
答案 1 :(得分:0)
你声明你的功能错了:
它应该返回int[][]
而不是int[]
。
此外,newMatrix[i]
代表一维数组。你不能把Integers放进去。请改用newMatrix[i][j]
。