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
  • 7,308
  • 19
  • 83
  • 134
  • 1
    I think you want an `ArrayList>`. –  Jun 07 '13 at 20:40
  • No like I said I am fine with ArrayList[][] i2DArrayList – C graphics Jun 07 '13 at 20:41
  • Unfortunately [you can't create array with generic type](http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#FAQ104). I hope you will end up with `ArrayList>` since it is the best approach for you. – Pshemo Jun 07 '13 at 20:47
  • I edited my question @Pshemo : I already know how to do it in 1D ... – C graphics Jun 07 '13 at 20:54
  • @Cgraphics But this will lead to type safety issues. If you want to ignore them then OK, it's your choice. – Pshemo Jun 07 '13 at 20:58

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
  • 54,340
  • 18
  • 130
  • 181
Mattias Buelens
  • 19,609
  • 4
  • 45
  • 51
  • 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