for-each 不适用于表达式类型 required array or java.lang.iterable
for-each not applicable to expression type required array or java.lang.iterable
我尝试将 Web 应用程序中的 json 消耗到 codeName1 中,因此,我必须阅读 json,为此我使用了 for-each 循环,但我在编译:
for-each not applicable to expression type required array or java.lang.iterable
我的代码:
package service;
import com.codename1.io.CharArrayReader;
import com.codename1.io.JSONParser;
import com.codename1.ui.List;
import java.util.ArrayList;
import entity.User;
import java.io.IOException;
import java.util.Map;
public class UserService {
public ArrayList<User> getAll(String json) {
ArrayList<User> u = new ArrayList<>();
try {
JSONParser j = new JSONParser();
Map<String, Object> p = j.parseJSON(new CharArrayReader(json.toCharArray()));
List<Map<String, Object>> list = (List<Map<String, Object>>) p.get("root");
for (Map<String, Object> obj : list) {
//...
}
} catch (Exception ex) {
}
return u;
}
}
这里:
import com.codename1.ui.List;
您使用的是错误的列表类型!
应该是
import java.util.List;
相反。
或者,反过来说:如果你想将 "custom" class com.codename1.ui.List
与 for-each 一起使用,那么 class 必须实现Iterable接口。
您应该删除导入 com.codename1.ui.List;
并用 java.util.List
更新它。
查看您的列表 import com.codename1.ui.List;
。实现 Iterable 的不是 java.util.List。
public interface List<E>
extends Collection<E>
并且 Collection 实现了 Iterable。
public interface Collection<E> extends Iterable<E>
或者,您可以使自定义列表实现 Iterable。
所以你的 class 应该看起来像 com.codename1.ui.List implements Iterable {}
.
我尝试将 Web 应用程序中的 json 消耗到 codeName1 中,因此,我必须阅读 json,为此我使用了 for-each 循环,但我在编译:
for-each not applicable to expression type required array or java.lang.iterable
我的代码:
package service;
import com.codename1.io.CharArrayReader;
import com.codename1.io.JSONParser;
import com.codename1.ui.List;
import java.util.ArrayList;
import entity.User;
import java.io.IOException;
import java.util.Map;
public class UserService {
public ArrayList<User> getAll(String json) {
ArrayList<User> u = new ArrayList<>();
try {
JSONParser j = new JSONParser();
Map<String, Object> p = j.parseJSON(new CharArrayReader(json.toCharArray()));
List<Map<String, Object>> list = (List<Map<String, Object>>) p.get("root");
for (Map<String, Object> obj : list) {
//...
}
} catch (Exception ex) {
}
return u;
}
}
这里:
import com.codename1.ui.List;
您使用的是错误的列表类型!
应该是
import java.util.List;
相反。
或者,反过来说:如果你想将 "custom" class com.codename1.ui.List
与 for-each 一起使用,那么 class 必须实现Iterable接口。
您应该删除导入 com.codename1.ui.List;
并用 java.util.List
更新它。
查看您的列表 import com.codename1.ui.List;
。实现 Iterable 的不是 java.util.List。
public interface List<E>
extends Collection<E>
并且 Collection 实现了 Iterable。
public interface Collection<E> extends Iterable<E>
或者,您可以使自定义列表实现 Iterable。
所以你的 class 应该看起来像 com.codename1.ui.List implements Iterable {}
.