简体   繁体   English

Java - 是ArrayList <Integer> [] []有可能吗?

[英]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? 不起作用,我开始认为甚至不可能将ArrayLists存储在矩阵中?

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. 这意味着您的列表将包含整数列表作为其值。

泛型和数组通常不能很好地混合,但这将起作用(发出警告,可以安全地忽略):

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: 如果要将列表存储在数组中,则仍需要将声明与inizialization分开:

ArrayList<Integer>[][] matrix = new ArrayList[10][10];

will specify a 2-dim-array of ArrayList objects. 将指定一个2-Dim-Array的ArrayList对象。

matrix[0][0] = new ArrayList<Integer>();

will initialize one specific cell with a new ArrayList of Integers. 将使用新的整数ArrayList初始化一个特定单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM