【发布时间】:2015-12-07 14:52:07
【问题描述】:
所以我有一个名为 Item 的对象数组,我想使用 for each 循环来遍历这个数组。
private ArrayList carriedItems;
/**
* this private helper method checks the ArrayList for the requested Item name. If found, return the Item.
* If not found, return null.
* @param the name of the item
* @return the item object
*/
public Item cherForItem(String name){
for(Item i: carriedItems)
if(name.compareTo(i.getName())==0)
return i;
return null;
}
当我编译时,我得到一个不兼容的类型:java.lang.Object 无法转换为 Item。我知道这意味着它需要一个 Item 对象,但我认为 for each 循环中的最后一个词是您正在经历的一系列事情?我很困惑。
【问题讨论】: