简体   繁体   English

Eclipse 编译错误显示不正确(只能迭代数组或 java.lang.Iterable 的实例)

[英]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.

相关问题 只能迭代一个数组或java.lang.Iterable的实例 - Can only iterate over an array or an instance of java.lang.Iterable 只能迭代java.lang.Iterable的数组或实例吗? - Can only iterate over an array or an instance of java.lang.Iterable? 错误:只能遍历数组或java.lang.Iterable的实例 - Error: can only iterate over an array or an instance of java.lang.Iterable 规则编译错误:只能迭代java.lang.Iterable的数组或实例 - Rule Compilation error : Can only iterate over an array or an instance of java.lang.Iterable 在reducer中的for循环上获得编译错误“只能迭代数组或java.lang.Iterable实例” - Getting compilation error “Can only iterate over an array or an instance of java.lang.Iterable” at for loop in reducer 只能在java.lang.Iterable中的数组或实例上进行迭代 - Can only iterate over an array or an instance of java.lang.Iterable in java 使用FileUtils时只能迭代数组或java.lang.Iterable的实例 - Can only iterate over an array or an instance of java.lang.Iterable while using FileUtils 在包含私有集合的类上使用迭代器错误“只能迭代数组或java.lang.Iterable的实例” - Error “Can only iterate over an array or an instance of java.lang.Iterable” using iterator on a class that contains a private collection 另一个“只能迭代数组或java.lang.Iterable的实例”问题 - Yet another “Can only iterate over an array or an instance of java.lang.Iterable” issue ForStatement-只能迭代数组或java.lang的实例。在csv文件创建中可迭代 - ForStatement - Can only iterate over an array or an instance of java.lang.Iterable in csv file creation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM