0

I'm having a arraylist which I need to specify the type in runtime.

ArrayList<String> alist = new ArrayList<String>();

I need to specify the type "String" at runtime. how can I do that. It should not be static.

Santhosh
  • 19,616
  • 22
  • 63
  • 74
  • 1
    see überSkeet's or Darren Gilroy's answer to http://stackoverflow.com/questions/1127923/specifying-generic-collection-type-param-at-runtime-java-reflection – Lars Andren May 12 '10 at 06:29
  • 1
    "I need to specify the type "String" at runtime" - basically you don't, you need to explain what you are trying to do in more detail. – Peter Lawrey May 12 '10 at 07:01
  • I don't follow this question. Are you trying to create an `ArrayList<>` without knowing the generic argument at compile time? Doesn't really make any sense for a static typing feature (erasure seems irrelevant, btw). If the enclosing instance or method defines a generic parameter then that might be what you want. – Tom Hawtin - tackline May 12 '10 at 11:58

3 Answers3

5

Thats no possible due to type erasure — a process where the compiler removes all information related to type parameters and type arguments within a class or method.

It's only available during compile time, to check types.

EDIT: As a workaround to your problem you could use ArrayList<Object> this would allow you to add any types to the ArrayList. In this case you could check types using instanceof and cast to concrete types.

stacker
  • 68,052
  • 28
  • 140
  • 210
1

You mean you want to parameterize the type used to create the ArrayList?

public <T> List<T> interestingMethod(Class<T> type) {
    List<T> aList = new ArrayList<T>();
    // do something interesting...
    return aList;
}

Passing the type argument is only needed for type inference and I find this pattern a kludge, but it's the only way with Java.

As the everyone else will point out, we don't have runtime type information with generics because of type erasure.

SteveD
  • 5,396
  • 24
  • 33
  • What does this syntax mean: `public List interestingMethod(Class type)`? I haven't come across a lone type parameter (the first ``) before. – Eric May 12 '10 at 06:52
  • It's because the method is generic, but the class it belongs is not generic. In this case, we need to declare the generic type (and any constraints on it such as extends or super). – SteveD May 12 '10 at 06:56
1

The generic type parameter is not compiled into the bytecode, therefore it is not available at runtime. An ArrayList<String> is simply an ArrayList at runtime. The closest thing you can achieve, is to add runtime checks yourself. For example, Collections class provides a decorator that does exactly this:

List l = Collections.checkedList(new ArrayList<String>(), String.class);
l.add("Jave uses erasure");
l.add(14);

If the list was created simply as ArrayList<String>, the two additions would succeed at runtime. With the wrapper implementation however, every item addition is validated, so the second call will cause a ClassCastException.

Eyal Schneider
  • 22,166
  • 5
  • 47
  • 78