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>();
    }
5
  • 1
    I think you want an ArrayList<ArrayList<Integer>>. Commented Jun 7, 2013 at 20:40
  • No like I said I am fine with ArrayList<Integer>[][] i2DArrayList Commented Jun 7, 2013 at 20:41
  • Unfortunately you can't create array with generic type. I hope you will end up with ArrayList<ArrayList<Integer>> since it is the best approach for you. Commented Jun 7, 2013 at 20:47
  • I edited my question @Pshemo : I already know how to do it in 1D ... Commented Jun 7, 2013 at 20:54
  • @Cgraphics But this will lead to type safety issues. If you want to ignore them then OK, it's your choice. Commented Jun 7, 2013 at 20:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Dammit, Java and its crappy generics again.
Not a problem of generics...
Updated my answer. @Legend, what do you mean exactly?
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.
@Legend Exactly, I fell for that pitfall at first. Here's an example demonstrating why you cannot do that. What other error where you seeing?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.