View Full Version : simple java arraylist problem

April 3rd, 2011, 09:15 AM
Suppose I have an arraylist:
ArrayList<Type> a = new ArrayList<Type>();

I would like to know the type, but


returns java.util.ArrayList

any ideas how i can get the type? the arraylist is initialy empty..


April 3rd, 2011, 12:47 PM
<type> should have been specified when you created the ArrayList, so you should already know what it is. That's how I've always used it, at least.

April 3rd, 2011, 12:51 PM
You should be able to get that information using reflection. The type of a generic ArrayList is available at compile time but not at runtime so I don't think you can call a method to get the type. But with reflection you may be able to obtain it. You could also store the type separately in a variable (Type.class).

Some Penguin
April 3rd, 2011, 08:02 PM
See http://i-proving.ca/space/BC+Holmes/blog/2008-11-18_2.

April 4th, 2011, 07:10 AM
wrap it in a class and use your class. Then you can add all sorts of stuff to it.

But that is the type, what its returning its the name of the type.