-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.

OcelotXL
  • 159
  • 2
  • 6
  • 14
  • 1
    Ok, here's one suggestion - Try it out on your own first. And if you can't seem to solve it, post what you tried here, and we will help you solve the issue. – Rohit Jain Feb 11 '13 at 08:28
  • 1
    You need two loops. Try something. – JB Nizet Feb 11 '13 at 08:28
  • No attempt to do it yourself gets you -1 from me. – ATrubka Feb 11 '13 at 08:29
  • And while you [try it yourself](http://whathaveyoutried.com), you might look at [this question from the "related" area to the left](http://stackoverflow.com/questions/718554/how-to-convert-an-arraylist-containing-integers-to-primitive-int-array?rq=1) to get some hints. Good luck! – Joachim Sauer Feb 11 '13 at 08:30
  • With, err, native Java? No framework library? Yeah, that's a challenge. ;) (sorry, two loops and your done. `for` is native Java) (OK - you may be looking for some build-in converter but there isn't one. We need to copy the values *manually*. Any 3rd party util would do the same behind the scenes) – Andreas Dolk Feb 11 '13 at 08:31
  • Loks like it is syntax problems you can't handle. **HINT:** you need to place elements as `newMatrix[i][j] = value` – svz Feb 11 '13 at 08:46
  • what is the content of ArrayList cols? can you post sample content of both arraylist? – Fathah Rehman P Feb 11 '13 at 09:32

2 Answers2

0

I think this can be done easily by looping through arraylist.

Fathah Rehman P
  • 8,401
  • 4
  • 40
  • 42
0

Your declared your function wrong:

It should return int[][] instead of int[].

Additionally, newMatrix[i] represents a 1D array. You can't put Integers in it. Use newMatrix[i][j] instead.

alan.sambol
  • 265
  • 3
  • 14