I wanna use an ArrayList for my Code but it should contain different types of Objects. So it should be sth like this:
switch (foo) {
case "foo1":
list=new ArrayList<foo1>();
break;
case "foo2":
list = new ArrayList<foo2>();
break;
I know the posted Code does not work but is it possible to make sth similar.
And before you ask there should be multiple actions performed on this List but i dont wont to write everything again just with a different name for the List.
public class foo1 {}
public class foo2 {}
import java.util.ArrayList;
public class work{
public static void main(String[] args){
ArrayList<?> list;
switch (args[0]) {
case "foo1":
list=new ArrayList<foo1>();
break;
case "foo2":
list= new ArrayList<foo2>();
break;
default:
System.out.println("Some Error Text");
return;
}
list.add(new foo1());
}
}