【发布时间】:2013-08-22 06:26:11
【问题描述】:
假设我有一堂课。它被称为项目。
public class Item {
public boolean usable = false;
protected boolean usage;
public int id;
public String name;
public String type;
public int stacknum;
protected int tier;
public String rarity;
boolean equipable;
boolean equiped;
String altName;
public Item(int idIn, String nameIn, String typeIn) {
usage = false;
id = idIn;
name = nameIn;
type = typeIn;
stacknum = 0;
tier = 0;
rarity = "Common";
}//end of constructor
}//end of class
假设我有一个名为:
Inventory = new Item[5];
它包含这些元素:
Item elementOne = new Item(1, "Element One", "Array Element");
Item elementTwo = new Item(2, "Element Two", "Array Element");
等等
Inventory[0] = elementOne;
Inventory[1] = elementTwo;
Inventory[2] = elementThree;
等等。我将如何编写一种方法来找出数组中的哪个元素 Item (或一般的任何东西)是 I.e.
elementOne.findPlace
将返回 0 的 int 值。
谢谢!
【问题讨论】:
-
为什么不覆盖
equals(Object o)并将List<T>与contains(T item)或indexOf(T item)一起使用? -
我不完全明白你的意思。你能解释一下吗?
-
List<T>中有一些有用的方法可以做你想做的事情(即indexOf(I item))。如果您在您的类Item中实现equals(Object o)方法,则可以使用它。我可以理解吗? -
是的,这样更有意义,谢谢