Fel*_*ore 1 java arrays foreach
所以我有一个我所做的一个名为Item的对象数组,我想用每个循环来通过这个数组.
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;
}
Run Code Online (Sandbox Code Playgroud)
当我编译时,我得到一个不兼容的类型:java.lang.Object无法转换为Item.我知道这意味着它期待一个Item对象,但我认为每个循环中的最后一个字是你要经历的事情的数组?我很困惑.
你应该像这样定义你的arraylist:
private ArrayList<Item> carriedItems;
Run Code Online (Sandbox Code Playgroud)
您没有指定您的arraylist将持有的数据类型...
| 归档时间: |
|
| 查看次数: |
414 次 |
| 最近记录: |