0

I'm trying to create a 2D array of lists for a Sudoku. Essentially 81 lists each containing possible solutions to that box in the Sudoku grid. I've tried multiple declarations so far, but whenever I try to add values to a list it returns a null pointer exception. Here is an example, simply populating each of the lists with the numbers 1-9.

List<Integer>[][] sudoku = (List<Integer>[][]) new List[9][9];

for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            for (int k = 1; k < 10; ) {
                sudoku[i][j].add(k);
            }
        }
}

I'm not even positive a 2D array of lists is the optimal way to go about this, but I've done everything from scratch (with a relatively low knowledge of java) so far so I'd like to follow through with this method. The original code looked as follows:

List[][] sudoku = new List[9][9];

Research quickly revealed that this wouldn't cut it.

Thank you in advanced for any help!

Elijah
  • 223
  • 3
  • 8
  • 1
    Try `List> sudoku = new List<>();` instead. I think You cannot make an array of collections. – Noe Apr 10 '13 at 14:49

4 Answers4

4

Try this one. The general idea, create a master list and while you loop through it, create one inner list.

    /* Declare your intended size. */
    int mainGridSize = 81;
    int innerGridSize = 9;

    /* Your master grid. */
    List<List<Integer>> mainList = new ArrayList<List<Integer>>(mainGridSize);

    /* Your inner grid */
    List<Integer> innerList = null;

    /* Loop around the mastergrid */
    for (int i=0; i<mainGridSize; i++) {

        /* create one inner grid for each iteration of the main grid */
        innerList = new ArrayList<Integer>(innerGridSize);

        /* populate your inner grid */
        for (int j=0; j<innerGridSize; j++) 
            innerList.add(j);

        /* add it to your main list */
        mainList.add(innerList);
    }

Illustrated:

enter image description here

If you need to vary your grid, just change the values of the gridSize.

Jops
  • 22,535
  • 13
  • 46
  • 63
2

You cannot create array of generic lists.

You can create List of Lists:

List<List<List<Integer>>> soduko = new ArrayList<>();

And then populate it as you wish.

or use casting:

List[][] soduko = (List<IntegerNode>[][]) new LinkedList[9][9];
Community
  • 1
  • 1
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
0

You have create the array of Lists but you did not initialize it. Insert this in the second line an the problem should be solved.

for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++){
         sudoku[i][j]=new ArrayList<Integer>(); 
    }
}

Or to do it all in one go do it like this:

for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
        sudoku[i][j]= new ArrayList<Integer>();
        for (int k = 1; k < 10; ) {
            sudoku[i][j].add(k);
        }
    }
}
DeltaLima
  • 5,864
  • 1
  • 16
  • 32
  • Worked well as soon as I changed "List()" to "ArrayList()" seeing as the List isn't considered a concrete class. Thank you kindly. – Elijah Apr 10 '13 at 15:36
  • Yeah of course! Sorry for the confusion. Updated my answer accordingly – DeltaLima Apr 10 '13 at 15:39
  • Hakuna Matata, your answer helped me iron out the null pointer and the Sudoku solver worked flawlessly on the first time through :) – Elijah Apr 10 '13 at 15:41
0

If you know that you need exactly 81 2D arrays, you can create 3D array:

int[][][] sudoku = new int[81][9][9];

The way you did it now would produce compilation error.

Vitaly
  • 2,760
  • 2
  • 19
  • 26