eclipse Java 7 Diamond 运算符上的编译错误:ArrayList<>();
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12630006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compile Error on Java 7 Diamond Operator: ArrayList<>();
提问by Simulant
I have this line of code:
我有这行代码:
List<IObserver<?>> observers = new ArrayList<>();
and get the 3 following Errors:
并得到以下 3 个错误:
Cannot instantiate the type ArrayList<?>
Syntax error on token "<", ? expected after this token
Type mismatch: cannot convert from ArrayList<?> to List<IObserver<?>>
I am using Eclipse 3.7, I installed JDK 7 update 5 and the Project is set to use the JRE System Library[JavaSE1.7] in the Build Path.
我使用的是 Eclipse 3.7,我安装了 JDK 7 update 5,并且项目设置为在构建路径中使用 JRE 系统库 [JavaSE1.7]。
Passing in the IObserver<?>
on the right side compiles fine, but I have to use the diamond operator.
传入IObserver<?>
右侧的编译很好,但我必须使用菱形运算符。
I think this is a configuration problem, but I can't figure out what I have missed.
我认为这是一个配置问题,但我无法弄清楚我错过了什么。
回答by obe6
The code should work : diamond operator is used correctly. I suggest you to install a more recent version of Eclipse (Indigo or Juno), and set the compiler compliance level to 1.7.
代码应该可以工作:正确使用了菱形运算符。我建议您安装更新版本的 Eclipse(Indigo 或 Juno),并将编译器合规性级别设置为 1.7。
Here is a simple working example (IObserver is invented here). Print to the console : "we are 2"
这是一个简单的工作示例(此处发明了 IObserver)。打印到控制台:“我们是 2”
package it.ant.test;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<IObserver<?>> observers = new ArrayList<>();
IObserver<String> stringObserver = new Observer<>();
IObserver<Integer> integerObserver = new Observer<>();
stringObserver.addObserved("we are ");
integerObserver.addObserved(2);
observers.add(stringObserver);
observers.add(integerObserver);
for (IObserver<?> o : observers) {
System.out.print(o.getObserved());
}
}
}
interface IObserver<T> {
void addObserved(T t);
T getObserved();
}
class Observer<T> implements IObserver<T> {
private T observed;
@Override
public void addObserved(T observed) {
this.observed = observed;
}
@Override
public T getObserved() {
return observed;
}
}
回答by Rohit Jain
Diamond operator will work only when you don't have wildcard
as your generic type in you LHS..
Diamond 运算符仅wildcard
在您的 LHS 中没有通用类型时才起作用。
List<IObserver<?>> observers = new ArrayList<>();
In this code, Compiler sees the LHS, and is satisfied that it can be List
of IObserver
of anytype
..
在这段代码,编译器看到的LHS,并满足它可以List
的IObserver
的anytype
..
But, at runtime, you need to have the actual type for this anytype
..
但是,在运行时,您需要拥有此anytype
..的实际类型。
Had you not used the Wildcard
on LHS, it would have worked.. Actually your above code is equivalent to (If we see it as prior to Java 7): -
如果您没有Wildcard
在 LHS 上使用它,它会起作用.. 实际上,您上面的代码等效于(如果我们将其视为 Java 7 之前的代码):-
List<IObserver<?>> observers = new ArrayList<IObserver<?>>();
As RHS generic type is inferred from the LHS..Now you see the problem?? You don't have a Concrete Type
to make object
of IObserver
on RHS..
由于 RHS 泛型类型是从 LHS 推断出来的..现在你看到问题了吗?你在 RHS 上没有什么Concrete Type
可做object
的IObserver
..