开发者

Java - Is ArrayList<Integer>[][] possible?

开发者 https://www.devze.com 2023-03-22 12:56 出处:网络
ArrayList<Integer>[][] matrix = new ArrayList<Integer]>[sizeX][sizeY](); or ArrayList<Integer>[][] matrix = new ArrayList<Integer]>()[sizeX][sizeY];
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.

0

精彩评论

暂无评论...
验证码 换一张
取 消