Is ArrayList <Integer> [] [] possible?

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

or

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

doesn't work, I'm starting to think that it can't even store ArrayLists in a matrix?

+6
source share
5 answers

If you still want to use arrays too:

  ArrayList<Integer>[][] matrix = new ArrayList[1][1]; matrix[0][0]=new ArrayList<Integer>(); //matrix[0][0].add(1); 
+7
source

Try

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

More on List

+1
source

Use it

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

This means that the list will consist of a list of integers as its value.

+1
source

Usually, generators and arrays do not mix well, but this will work (gives a warning that can be safely ignored):

 ArrayList<Integer>[][] matrix = new ArrayList[sizeX][sizeY]; 
0
source

If you want to store the list in an array, you still have to separate the declaration and initialization:

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

will indicate a 2-dimensional array of ArrayList objects.

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

initializes one specific cell with a new ArrayList of integers.

0
source

Source: https://habr.com/ru/post/893489/


All Articles