Java: 2D array of arraylists? -
i working on sudoku solving program , need arraylist holds numbers 1 thru 9 each of squares on 9x9 board. each of these arraylists correspond possible numbers go in square, if number can not go in square, removed list.
i want able pull arraylist of current square working on, example if wanted remove number 7 arraylist corresponding square (3,5)
arrayoflists[3][5].remove(integer.valueof(7));
however can't figure out how this. when try create array getting error on line declare array of arraylists
cannot create generic array of arraylist
here code:
//create arraylist arraylist<integer> nums = new arraylist<integer>(); //fill arraylist numbers 1-9 (int = 1; < 10; i++) { nums.add(i); } //create 9x9 array of arraylists arraylist<integer>[][] array = new arraylist<integer>[9][9]; //fill each element of array arraylist of numbers 1-9 for(int = 0; i<9; i++){ for(int j = 0; j<9; j++){ array[i][j] = nums; } } }
am doing incorrectly or not possible create array of arraylists? if not possible, how should then?
anytime see list of lists, alarm bells start ringing. situations want such thing rare indeed, , not 1 of them.
you've got fixed board consisting of 9 fixed squares, columns , rows, each position of may take number 1-9.
use array of these concepts, because fixed in size , need direct access each element - collections offer no benefit , hindrance. use logic (possibly sets) ensure numbers used once in each zone.
Comments
Post a Comment