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?
A fundamental limitation of ArrayLists is that they can only hold objects, and not primitive types such as ints. To retrieve items from an ArrayList, use the get() method. To determine the size of an ArrayList, use the size() method.
Java ArrayList is an ordered collection. It maintains the insertion order of the elements. You cannot create an ArrayList of primitive types like int , char etc. You need to use boxed types like Integer , Character , Boolean etc.
ArrayList<Integer> numbers = new ArrayList<>(Arrays. asList(1, 2, 3, 4, 5, 6)); This is how you declare an ArrayList of Integer values. You can do the same to create an ArrayList with String objects as well, e.g.
ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Returns: It returns true if the specified element is found in the list else it returns false.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With