我很困惑..。我知道我可以把ArrayList of Lists存储在List of Lists上;
我有一个这样的物体:
SemanticTuple< List<String>, List<List<String>> > object;我可以这么做:
List<List<String>> list = new ArrayList<>();
object = new SemanticTuple<>("name", Arrays.asList(header.split(headerSplitter)), list);但我不能这么做
object = new SemanticTuple<>("name", Arrays.asList(header.split(headerSplitter)), new ArrayList<>());为什么它认不出那里的类型?
更新
public class SemanticTuple <HASH, DATA> implements Serializable {
private String name;
private HASH hash;
private DATA Data;
public SemanticTuple() {
}
public SemanticTuple(String name, HASH hash, DATA Data) {
this.name = name;
this.hash = hash;
this.Data = Data;
}
...对象被声明为来自另一个类的泛型:
public class MeteorologicTask extends Task < LineNumberReader,
SemanticTuple< List<String>, List<List<String>> >,
SemanticTuple< List<String>, List<List<String>> > >{
...从..。
public abstract class Task <RESOURCE, INPUT, OUTPUT> implements Callable<OUTPUT>{
protected RESOURCE resource;
protected INPUT input;
protected OUTPUT output;
protected Integer taskID;
public Task() {
}
public Task(RESOURCE resource, Integer taskID) {
this.resource = resource;
this.taskID = taskID;
}
...图片:

发布于 2015-10-25 21:13:42
您正在尝试创建一个新的ArrayList,而不指定元素类型。您是否尝试过显式地指定它?
SemanticTuple<List<String>, List<List<String>>> object =
new SemanticTuple<List<String>, List<List<String>>>(
"name",
Arrays.asList(header.split(headerSplitter)),
new ArrayList<List<String>>()
);编辑:在SemanticTuple的构造函数调用中显式声明泛型类型似乎在Java7中有效。
发布于 2015-10-25 21:20:09
在第一种情况下,推导出包含元素的类型,详细说明赋值的左侧,即List<String>。因此,当您将list作为参数传递时,它的静态类型是明确分配的。在第二条指令中,编译器很可能断言它无法推断包含的类型,这必须在编译时决定。
https://stackoverflow.com/questions/33335100
复制相似问题