我有一个Java代码:
for(Iterator it = c.getArrayList().iterator(); it.hasNext(); ) {
Object i = it.next();
// Here I have an error, i is not a boolean
if (i) {
System.out.format("Delete %s%n", i);
it.remove();
}
else {
System.out.println("End");
break;
}
}
但是该if
子句会引发错误。它期望boolean
但Object
被给予。Java 不能转换类型,对吧?
我如何更改类型或我必须在if
子句中添加什么才能使其正常工作?
升级版:
它是字符串的集合(一个 ArrayList)。
Object i = it.next(); // I get one element from collection.
if (i) { // check if it's not the end, if it's not the last element of the collection