3

I want to pass a Class object to a function then declare a ArrayList of the type that class is. I am wondering how I do this.

public static void CreateList(Class c){
    // I want to create this array list using the variable c to define its type
    ArrayList<c> list = new ArrayList();
}
6
  • 1
    You can't and it makes no sense to do so. Commented Mar 14, 2015 at 16:36
  • 1
    Not in that example but if I had a List of some Superclass that I wanted to find instances of a subclass within it. public static ArrayList<? extends Object> Get Objects(ArrayList<? extends Object> list, Class c) Stop being a party pooper all the time Sotirios Commented Mar 14, 2015 at 16:38
  • 3
    Why not use a generic method definition? public static <T> void createList(T c) Commented Mar 14, 2015 at 16:41
  • Please post your actual use case in your question. The answer to your current question is: you can't. Commented Mar 14, 2015 at 16:43
  • 2
    Fair enough, stop telling everyone they're non-logical. Commented Mar 14, 2015 at 16:46

2 Answers 2

6

You can genericize the Class to Class<T> and then use T:

public static <T> void createList(Class<T> cls) {
    ArrayList<T> = new ArrayList<>();
}

To expand, if you need to find the all instances of a class, or its subclasses, from a list and return the resulting values as a list, you can do:

public static <T> List<T> filterInstancesOf(List<?> original, Class<T> cls) {
    List<T> rv = new ArrayList<>();
    for (Object o: original) {
        if (cls.isInstance(o)) {
            rv.add(cls.cast(o));
        }
    }
    return rv;
}

There were suggestions that this cannot be done, and does not make sense to do, but it does have very much sense.

It was also suggested that the original parameter use a bounded type, say List<? super T>. Again this would be useless, it would not add to the type safety, but actually render the method itself less useful.

As it is now, you can use the method to filter instances that implement an interface, for example, assuming we have

List<SomeClass> lst;
...
filterInstancesOf(lst, Serializable.class);

If the method used bounded types, this could not be used, as SomeClass is not the superclass of Serializable.

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

5 Comments

You don't even need to pass the class object as it is not used by itself.
It provides the type.
You could use a bounded type paramater to ensure that cls is actually a subtype of the element type of the list.
Yes but it is not needed either; now the cls can be an interface too.
You're right in that what you have is more flexible in that it allows an interface. But the OP's comment asked about a subclass. A bounded type parameter (note, not a bounded wildcard) could enforce the restriction to a subclass at compile time, if that's really what the OP wanted.
-1

Use

Array.newInstance(Class<?> componentType, int length)

and then create a list from it:

Arrays.asList(array)

3 Comments

This is not an ArrayList
@SafeVarargs @SuppressWarnings("varargs") public static <T> List<T> asList(T... a) { return new ArrayList<>(a); }
The ArrayList that Arrays.asList creates is a nested class of Arrays. That is, it's a java.util.Arrays$ArrayList and not a java.util.ArrayList. They differ in that the list returned by Arrays.asList does not permit elements to be inserted or removed. (Yeah, this is really confusing.)

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.