1

Can I get Class<T> from Class<T[]>?

public static <T> void doSometing(final Class<T[]> arrayType) {

    final Class<T> elementType; // = @@?
}

Or, can I get Class<T[]> from Class<T>?

2
  • Can you please explain the use case for this Commented Jun 20, 2013 at 3:03
  • I rolled back your update since it ended up taking the form of a new question. Commented Jun 20, 2013 at 14:28

1 Answer 1

5

You can use Class.getComponentType(), although there's no way for that method to give you generic type safety, so you'll need to do an unchecked cast to get a Class<T>:

public static <T> void doSomething(final Class<T[]> arrayType) {

    @SuppressWarnings("unchecked")
    final Class<T> componentType = (Class<T>)arrayType.getComponentType();

    //etc.
}

For the reverse, see this question: Obtaining the array Class of a component type

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

2 Comments

Can you please take a look at this following question? stackoverflow.com/questions/17204788/…
@JinKwon Sorry I didn't see that sooner. Looks like it's been answered pretty well.

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.