我想为我的代码使用ArrayList,但它应该包含不同类型的对象。 所以它应该是某事像这样:Java:同名,但不同的通用类型
switch (foo) {
case "foo1":
list=new ArrayList<foo1>();
break;
case "foo2":
list = new ArrayList<foo2>();
break;
我知道贴的代码不工作但有可能使某物相似。
而你问之前应该有多个行动在这个名单上执行,但我不会再写一切,只是用一个不同的名称名单。
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());
}
}
调查通配符。 –
这是可能的,但可能不是很有用。它看起来像[XY问题](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。你能描述一下你想如何使用对这些列表的引用吗? – Pshemo
发布的代码将在Java 7中工作 – nikis