Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList of ArrayList of ints in Java?

I am working on writing a sudoku solver and I want the grid to be stored as an arraylist of arraylist of ints...each spot will have an arraylist of ints of all possible numbers (or the definite value).

ArrayList<ArrayList<int>> sudoku_board = new ArrayList <ArrayList<int>>();

Java is throwing me an error saying "dimensions expected after token" on the ints.

like image 865
john smith Avatar asked Dec 17 '25 19:12

john smith


1 Answers

Generic type parameters require reference types, rather than primitive types. Use

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

Also when coding to an interface use the interface as the reference type, in this case List. Everything that appears within the generics should remain as the implementation type due to the non co-variance of generics.

From @assylias comment, a more generic type of list is

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

This will allow for List implementations types other than ArrayList to be added should refactoring be necessary later.

like image 111
Reimeus Avatar answered Dec 20 '25 10:12

Reimeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!