Java 为什么我要不断地获得一个ArrayIndexOutofBoundsException?
我试图为我的CS课程编写一个模仿自动售货机动作的程序。我有一个双数组stock,表示特定“插槽”中的物品数量[我的自动售货机很奇怪,有点像一个长自动售货机,有一列不同的物品]。以下是我目前的代码:Java 为什么我要不断地获得一个ArrayIndexOutofBoundsException?,java,arrays,indexoutofboundsexception,Java,Arrays,Indexoutofboundsexception,我试图为我的CS课程编写一个模仿自动售货机动作的程序。我有一个双数组stock,表示特定“插槽”中的物品数量[我的自动售货机很奇怪,有点像一个长自动售货机,有一列不同的物品]。以下是我目前的代码: public class VendingMachine { // define fields here public static double itemPrice[]; public static String[] itemName; public static i
public class VendingMachine
{
// define fields here
public static double itemPrice[];
public static String[] itemName;
public static int stock[][];
public static int maxPerSlot;
public static double cashAmmount;
public VendingMachine(int numslots, int maxperslot, double cash)
{
final int numSlots = numslots;
maxPerSlot = maxperslot;
cashAmmount = cash;
stock = new int[numSlots][0];
itemPrice = new double[numSlots];
itemName = new String[numSlots];
// complete this method
}
public void setProduct(int slot, String product, double price)
{ int Slot = slot;
itemPrice[slot] = price;
itemName[slot] = product;
stock[Slot][0] = 0;
//
}
public void restockProduct(String product, int quantity)
{
String Product = product;
int currentCapacity = quantity - maxPerSlot;
for(int i = 0; i < stock.length; i++){
if (itemName[i]==Product){
for(;quantity <= maxPerSlot && currentCapacity != 0; quantity--)
stock[i][0] += 1;
}
}
//Put # of products in slot that holds it and if that slot is full put the rest in the next
//availble slot that holds that product, if all full return error.
}
public double getCashOnHand()
{
return cashAmmount; // replace this line with your code
}
public int getQuantity(int slot)
{
return stock[slot][1]; // replace this line with your code
}
public int getQuantity(String product)
{ int total = 0;
for (int i = 0; i<itemName.length;i++){
if (product == itemName[i]){
total += stock[i][1];
}
}
return total;
}
public boolean buyItem(int slot)
{ int snum = slot;
if (stock[snum][1] != 0){
stock[snum][1]--;
return true;
} else {
return false;} // replace this line with your code
}
}
公共类自动售货机
{
//在此定义字段
公共静态双项目价格[];
公共静态字符串[]itemName;
公共静态整数股票[];
公共静态int maxPerSlot;
公共静态双现金账户;
公共自动售货机(int numslots、int maxperslot、双倍现金)
{
最终整数numSlots=numSlots;
maxPerSlot=maxPerSlot;
现金金额=现金;
股票=新整数[numSlots][0];
itemPrice=新的双倍[numSlots];
itemName=新字符串[numSlots];
//完成此方法
}
公共产品(整数槽、字符串产品、双倍价格)
{int Slot=Slot;
itemPrice[插槽]=价格;
itemName[插槽]=产品;
股票[Slot][0]=0;
//
}
公共空再进货产品(字符串产品,整数数量)
{
字符串产品=产品;
int currentCapacity=数量-最大容量;
对于(int i=0;i
stock = new int[numSlots][0];
这定义了一个numSlot数组的数组,每个数组的长度为0。您在stock
的第二维中分配零个元素
stock = new int[numSlots][0];
因此,当您尝试访问索引为零的元素时,会出现该异常
stock[Slot][0] = 0;
这条线
stock = new int[numSlots][0]; // <-- A length of zero? You want a one there.
stock=newint[numSlots][0];//因为此行:
stock = new int[numSlots][0];
将stock
分配为数组数组,每个数组的长度都为0。因此,您无法将任何内容分配到这些数组中(它们没有任何要分配的元素)。因此,执行此操作时:
stock[Slot][0] = 0;
您将获得一个ArrayIndexOutOfBounds
。请记住,在Java中,索引从0开始,因此如果您想要一个索引从0到N的数组,则必须将数组的大小分配为N+1。在构造函数中初始化stock时,请执行以下操作:
stock = new int[numSlots][1];
使用0而不是1初始化长度为0的数组!因此您的主函数出现了一个错误,但您不打算将其包含在代码中?
stock = new int[numSlots][0];
stock[Slot][0] = 0;
stock = new int[numSlots][1];