Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 406
Sane Avatar asked Jul 25 '11 11:07

Sane


People also ask

Can ArrayList have integers?

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.

Can ArrayList be any type?

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.

How do you declare an ArrayList in integers?

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.

How do you check if an Integer is in an ArrayList Java?

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.


2 Answers

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);
like image 56
jerrevds Avatar answered Oct 14 '22 10:10

jerrevds


Try

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

Read more on List

like image 41
jmj Avatar answered Oct 14 '22 09:10

jmj