-5

I am interested in returning an int[][] via passing non-null parameters of single and dual ArrayLists to a class method, such as:

//outer loop
ArrayList<ArrayList<Integer>> rowsInMatrixA;
//inner loop
ArrayList<Integer>  colsInMatrixA;

using only native Java. I have searched high and low here, in Java API, Java tutorials, and many other places. All ideas and suggestions welcome.

EDIT: here's what I've tried:

   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;
}

it still doesn't work: I get an IndexOutOfBoundsException on this line when I compile and try to get a printout in main:

    newMatrix[i][j] = rows.get(i).get(j);

How can this be resolved? I would appreciate any help. Thanks.

4

2 回答 2

0

您声明您的功能错误:

它应该返回int[][]而不是int[].

此外,newMatrix[i]表示一维数组。你不能把整数放进去。改为使用newMatrix[i][j]

于 2013-02-11T08:47:48.317 回答
0

我认为这可以通过遍历arraylist来轻松完成。

于 2013-02-11T08:34:28.297 回答