[英]Eclipse compile error shown incorrectly (Can only iterate over an array or an instance of java.lang.Iterable)
I'm experiencing a really weird issue in Eclipse (Photon 4.8).我在 Eclipse (Photon 4.8) 中遇到了一个非常奇怪的问题。 I have some code that uses the for (Object x : ObjectList){} logic and all of a sudden it's throwing a compile error on me.
我有一些代码使用 for (Object x : ObjectList){} 逻辑,突然间它向我抛出编译错误。
Can only iterate over an array or an instance of java.lang.Iterable
So as to keep it super simple, I wrote the following as a test in my class为了保持超级简单,我在课堂上写了以下作为测试
ArrayList<String> tmp = new ArrayList<String>();
tmp.add("making sure there's something here");
tmp.add("and again...just for the heck of it");
for(String x : tmp) {
System.out.println(x);
}
That block also throws the same error (on the "tmp" object).该块也抛出相同的错误(在“tmp”对象上)。 I've restarted Eclipse several times and done a clean/rebuild.
我已经多次重新启动 Eclipse 并完成了清理/重建。 My Java compiler is set to 1.8 which is a change that i made about a week ago from 1.6.
我的 Java 编译器设置为 1.8,这是我大约一周前从 1.6 所做的更改。 But it's been compiling fine the past week with no errors.
但它在过去一周编译得很好,没有错误。 Just saw this pop up today out of the blue.
今天刚看到这个突然出现。
Seems like a bug in the Eclipse compiler, but I'm not sure how to resolve it.似乎是 Eclipse 编译器中的一个错误,但我不确定如何解决它。 Any help would be greatly appreciated.
任何帮助将不胜感激。
Adding "Minimal, Complete and Verifiable Example" below在下面添加“最小、完整和可验证的示例”
public class Test {
public static void main(String[] args) {
java.util.ArrayList<String> tmp = new java.util.ArrayList<String>();
tmp.add("String 1");
tmp.add("String 2");
for(String x : tmp) {
System.out.println(x);
}
}
}
The above class throws the following compile error for "tmp"上面的类为“tmp”抛出以下编译错误
Can only iterate over an array or an instance of java.lang.Iterable
You don't need to define a new iterator:您不需要定义新的迭代器:
ArrayList<String> tmp = new ArrayList<String>();
tmp.add("making sure there's something here");
tmp.add("and again...just for the heck of it");
for(String x : tmp) {
System.out.println(x);
}
>> making sure there's something here
>> and again...just for the heck of it
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.