Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
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?

share|improve this question
1  
What do you want to achieve? The two things you posted are clearly not possible, as they don't compile. Could you tell us what you want? – Joachim Sauer Jul 25 '11 at 11:28

5 Answers

up vote 4 down vote accepted

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);
share|improve this answer

Try

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

Read more on List

share|improve this answer
What I'm trying to do is to create a multidimensional array (matrix) where I have an ArrayList on each position. – Sane Jul 25 '11 at 11:15
ok. this is perfect for you then . – Jigar Joshi Jul 25 '11 at 11:18
Hard to be sure, but it sounds like the OP actually wants a 3d matrix: a 2d matrix whose cells are arraylists. – CPerkins Jul 25 '11 at 12:14

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.

share|improve this answer

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];
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.