【发布时间】:2016-06-04 06:46:31
【问题描述】:
大家好,我正在制作一个库存程序,但我在使用 stockItem 删除方法时遇到了问题。我希望它删除给定索引处的项目并返回已删除的项目。如果索引无效则返回null。
到目前为止,这是我的代码。请滚动到底部看看我在说什么。
import java.util.ArrayList;
public class Inventory {
private ArrayList<StockItem> stock;
public Inventory() {
stock = new ArrayList<StockItem>();
}
public void addStockItem(StockItem item) {
stock.add(item);
}
public int size() {
return stock.size();
}
public String toString() {
String result = "";
for(StockItem item: stock)
result+=item.toString()+"\n";
return result;
}
public boolean isValidIndex(int index) {
return index >=0 && index < stock.size();
}
public StockItem getItem(int index) {
if (index < 0 || index >= this.stock.size()){
return null;
}
return this.stock.get(index);
}
/**
*
* @param index
* @return null if index is invalid, otherwise
* remove item at the given index and return the
* removed item.
*/
public StockItem remove(int index) {
return null; //I need to do this part
}
}
【问题讨论】:
标签: java class object inventory