4

I have generic type with Class<T> object provided in constructor. I want to create two-dimensional array T[][] in this constructor, is this however possible?

9
  • Not in Java, only in languages with runtime generics like C#. Think about erasure. Commented Aug 7, 2013 at 18:28
  • Please post the specific signature of the constructor you're thinking of using. This may be possible, but it's not clear what you're asking. Commented Aug 7, 2013 at 18:30
  • 1
    Yes, you can use Array.newInstance(). See stackoverflow.com/questions/529085/…. I'd flag as a duplicate but I'm out of flags, can somebody take care of this? Commented Aug 7, 2013 at 18:31
  • 1
    @JasonC It's not exactly a duplicate, because this is asking about two-dimensional arrays and the question you linked doesn't seem to have any talk about creating multidimensional instances, which could lead someone to think it only works for one dimension and thus a more complicated procedure would be required than actually is. Commented Aug 7, 2013 at 18:40
  • 1
    It was closed as a duplicate, although I disagree now based on @JAB's comments above. Some of the other answers previously posted here (now deleted) demonstrated a lack of understanding that the 1D case described in the linked question could be extended to 2D - precisely the situation JAB described above. I've attempted to show that in my answer. I actually feel this question should be reopened and the other one be marked as a duplicate of this one, since this is a more general case. Commented Aug 9, 2013 at 16:57

2 Answers 2

17

Same as How to create a generic array in Java? but extended to 2D:

import java.lang.reflect.Array;

public class Example <T> {

    private final Class<? extends T> cls;

    public Example (Class<? extends T> cls) {
        this.cls = cls;
    }

    public void arrayExample () {
        // a [10][20] array
        @SuppressWarnings("unchecked")
        T[][] array = (T[][])Array.newInstance(cls, 10, 20);
        System.out.println(array.length + " " + array[0].length + " " + array.getClass());
    }

    public static final void main (String[] args) {
        new Example<Integer>(Integer.class).arrayExample();
    }

}

Note after reading JAB's comment above: To extend to more dimensions, just add []'s and dimension parameters to newInstance() (cls is a Class, d1 through d5 are integers):

T[] array = (T[])Array.newInstance(cls, d1);
T[][] array = (T[][])Array.newInstance(cls, d1, d2);
T[][][] array = (T[][][])Array.newInstance(cls, d1, d2, d3);
T[][][][] array = (T[][][][])Array.newInstance(cls, d1, d2, d3, d4);
T[][][][][] array = (T[][][][][])Array.newInstance(cls, d1, d2, d3, d4, d5);

See Array.newInstance() for details.

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

6 Comments

I noticed the OP edited the question and added "in this constructor". Same method as above (e.g. move the code from arrayExample() into the constructor).
Thank you, I didn't know (and I thought that I checked in documentation) that I can use newInstance with more parameters - my fault, I could check this out.
It is not safe to cast to T[][] (or whatever). cls may be a primitive type.
@newacct +1 but I'm out of votes. You would have to do something like new Example<Integer>(int.class) to get to that point, which is arguably weird, but yes, you are right. I'll update when I get a chance.
@JasonC: well, you could just return Object instead of a particular type of array. Or at least, the last level of the multidimensional array must be replaced with Object. (So, a two-dimensional array can be safely represented by Object[]; a one-dimensional array can only be represented by Object.)
|
2

You have to use reflection, but it's possible: http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Array.html#newInstance%28java.lang.Class,%20int...%29

Creates a new array with the specified component type and dimensions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.