0

I have a 2D matrix of size nXm for which each cell contains an unknown number of values of type Integer(therefore I have to use a List to be able to dynamically add stuff, and have to use a 2D array nXm because arrays are easy to access and write code). Pl

  • ease before recommend me any other data structure answer my question below and then discuss why I should not this and go for what you think will work better and more efficient:

How can I allocate memory to the variable below?

 ArrayList<Integer>[][] i2DArrayList;

I know at some points I have to do this. However prior to that I have to do some other memory allocation that I don't remember know. Could you guide me in this matter.

for (int i = 0; i < n; i++) {   
            for (int j = 0; j < m; j++) {
                i2DArrayList[i][j] = new ArrayList<Integer>();    
    }
}

I already know how to do it in 1D:

ArrayList<Integer>[] i1DArrayList;

i1DArrayList = new ArrayList[n]; 
    for (int i = 0; i < i1DArrayList.length; i++) {
        i1DArrayList[i] = new ArrayList<Integer>();
    }
C graphics
  • 6,750
  • 18
  • 75
  • 124

1 Answers1

1

Simply use a multidimensional array initializer:

ArrayList<Integer>[][] i2DArrayList = new ArrayList<Integer>[n][m];

which is equivalent to:

ArrayList<Integer>[][] i2DArrayList = new ArrayList<Integer>[n][];
for (int i = 0; i < n; i++) {   
    i2DArrayList[i] = new ArrayList<Integer>[m];
}

Java do not let you allocate generic arrays. That is, you cannot make a new T[] or new List<T>[]. The reason is that arrays also store their element type to allow type checking elements at run-time. However, type erasure removes these generic types at compile-time and thus no valid element type can be assigned to a new T[] or new List<T>[]. There are some solutions though:

  • Use some Collection type to store the matrix as well. For example:

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

    However, this gets ugly really fast.

  • You can make your own, non-generic entry class:

    class Entry {
        final List<Integer> entries = new ArrayList<Integer>();
    }
    
    Entry[][] i2DArrayList = new Entry[n][m];
    
  • If you know your matrix is sparse, you can use a Map<Position, List<Integer>> instead, with Position a value class with x and y fields.

Paul Bellora
  • 51,514
  • 17
  • 127
  • 176
Mattias Buelens
  • 17,720
  • 4
  • 40
  • 49
  • Dammit, Java and its crappy generics again. – Mattias Buelens Jun 07 '13 at 20:46
  • 1
    Not a problem of generics... –  Jun 07 '13 at 20:47
  • Updated my answer. @Legend, what do you mean exactly? – Mattias Buelens Jun 07 '13 at 20:57
  • The generics for your previous answer were fine. Not perfect, seeing as you don't have to specify the generics in the constructor. The code just wouldn't work. –  Jun 07 '13 at 21:00
  • @Legend No, previous version of answer wouldn't compile because of generics. Java wont something like `new ArrayList[n][]`. Take a look [here](http://stackoverflow.com/questions/7810074/array-of-generic-list) – Pshemo Jun 07 '13 at 21:04
  • 1
    @Legend Exactly, I fell for that pitfall at first. Here's [an example](http://stackoverflow.com/a/2927427/1321716) demonstrating why you *cannot* do that. What other error where you seeing? – Mattias Buelens Jun 07 '13 at 21:06