Java - Is ArrayList<Integer>[][] possible?
ArrayList<Integer>[][] matrix = new ArrayList<Integer]>[sizeX][sizeY]();
or
ArrayList<Integer>[][] matrix = new ArrayList<Integer]>()[sizeX][sizeY];
don't work, I'm starting to think that it's not even possible to store ArrayLists in a matrix开发者_JAVA百科?
If you still want to use and array:
ArrayList<Integer>[][] matrix = new ArrayList[1][1];
matrix[0][0]=new ArrayList<Integer>();
//matrix[0][0].add(1);
Try
List<List<Integer>> twoDList = new ArrayList<ArrayList<Integer>>();
Read more on List
Use this,
List<List<Integer>> matrix = new ArrayList<ArrayList<Integer>>();
It means that you list will be consisting of List of Integers as its value.
Generics and arrays generally don't mix well, but this will work (gives a warning, which can be safely ignored):
ArrayList<Integer>[][] matrix = new ArrayList[sizeX][sizeY];
If you want to store a list in an array then you still have to separate the declaration and the inizialization:
ArrayList<Integer>[][] matrix = new ArrayList[10][10];
will specify a 2-dim-array of ArrayList objects.
matrix[0][0] = new ArrayList<Integer>();
will initialize one specific cell with a new ArrayList of Integers.
精彩评论