Considering this part of a Java class,
private List<Object> components = new ArrayList<Object>();
private A a;
private B b;
private C c;
add(a, b, c);
public void add(Object... component){
for(Object o : component) {
components.add((CAST)o);
}
}
I would like to cast my Object o in the list, by the good type.
The expected result here would be to have A a, B b, C c in my list.
I try with getClass(), but I didn't compile. How can I upcast Objects, considering that I already know the expected Java type?
edit:
I need to add a first object of type A, a second object of type B, a second object of type C, ... (I don't know the number of items to add) if a list so that I will be able to get the list containing N objects correctly typed. (gtt(0) would be typed as A, get(1) as B, and get(2) as C )
Because I don't know the number of objects and that I initially don't know the object's type, I delclared my list as Object.
I created a method (add in that case). It varargs will help me for looping in the collection because I don't know the number of args.
I would simply like to get the first object, the second object and the third object (and so on) in my varargs, and to simply push it in my list, but not as Object type, as A, B or C, depend of what I received!
I don't know if it's more clear.