I have multiple classes (B, C and D) that are subclasses of class A. I need to make a List/Array containing B, C and D and create Objects based on whatever element I pull from the List/Array.
In AS3 I would do something like this: var classes:Array = [MovieClip, Sprite, Shape]; or a Vector of Classes.
How do I do this in Java? I'm thinking about something like this right now:
List<Class<? extends A>> list = new ArrayList<Class<? extends A>>();
list.add(B);
There are 5,000 or so classes built-in to Java, and programmers have written hundreds of thousands if not millions of their own.
The classes LinkedList, Stack, Vector, ArrayList, and CopyOnWriteArrayList are all the implementation classes of List interface that are frequently used by programmers. Thus there are four types of lists in Java i.e. Stack, LinkedList, ArrayList, and Vector.
The list is an interface in Java, which is a child interface of Collection. You can access it using java. util package. The classes that implement the List interface in Java are LinkedList, ArrayList, Vector, Stack, etc.
List<Class<? extends A>> classes = new ArrayList<Class<? extends A>>(); classes.add(B.class); classes.add(C.class); classes.add(D.class);
You can do analogues of both of those. As CarlosZ pointed out, there's List
and its various implementations, or you can create an array:
Class[] classes = new Class[] {
MovieClip.class, Sprite.class, Shape.class
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With