线程 "main" java.lang.ArrayIndexOutOfBoundsException 中的异常:9
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
public class ItemTable {
private final Integer[] idT = { 1,2,3,4,5,6,7,8,9,10 };
private final Integer[] quantityT = { 1,2,3,4,5,6,7,8,9,10 };
private final String[] barcodeT = { "001" , "002", "003", "004", "005",null,null,null,null,null, };
private final String[] nameT = { "Milk", "Bread", "Wine", "Butter", "Potato", "Sweet Potato","Ginger" ,"Avacado", "Cucumber"};
private final double[] perWeightT = {0.2,0.4,0.1,0.2,0.1,2,1,0.5,0,0,0};
private final double[] perPriceT = { 1.50, 2.45, 12.95, 4.75, 2, 3.5, 12, 4, 3, 1 };
private final ArrayList<ItemType> table = new ArrayList<>();
public final ArrayList<View> lookUpItem = new ArrayList<>();
public ItemTable()
{
for(int i = 0; i< idT.length; i++)
{
if(barcodeT[i]!=null)
table.add(new ItemTypeB (idT[i], nameT[i], perPriceT[i],perWeightT[i],barcodeT[i]));
else
{
if(perWeightT[i]!=0)
{
table.add( new ItemTypeW(idT[i], nameT[i], perPriceT[i],perWeightT[i],perWeightT[i]));
lookUpItem.add( new View(idT[i], nameT[i], "w"));
}
if(perWeightT[i]==0)
{
table.add( new ItemTypeC(idT[i], nameT[i], perPriceT[i],perWeightT[i], quantityT[i] ));
lookUpItem.add( new View(idT[i], nameT[i], "c"));
}
}
}
}
问题区域具体在最后一行代码附近的 table.add(new ItemTypeC)
。
您的一个数组 (nameT
) 是 "one-off";意思是:你所有的数组都有 10 个元素,但只有 9 个成员。
但实际问题的性质不同:您在这里做错了。您的程序是以 "procedural" 风格编写的。
含义:您有各种元素 "together" 形成有意义的东西。您的实施:将这些元素中的每一个放入其自己的数组中;并从每个数组中获取值 "build" 它们应该表示的意思 "together"。所以构成你的 "entity" 的东西只是 5 个不同数组中的索引。
在面向对象的方法中,您创建了抽象。像一个 class Product,它有多种属性;如"id"、"quantity"、"name"等。这也允许您创建合理的 equals 方法来轻松判断两个产品是否相同。当你创建了这样一个 class;然后创建 one 此类对象的数组。
长话短说:Java 中的编程是关于寻找和创建良好的抽象。如果你不这样做......你 运行 正是在这种低级问题中。
您的 nameT
数组只有 9 个元素。这会破坏您的代码。
当 i= 10 在 if(perWeightT[i]==0)
你的代码抛出 ArrayOutofBound 错误
public class ItemTable {
private final Integer[] idT = { 1,2,3,4,5,6,7,8,9,10 };
private final Integer[] quantityT = { 1,2,3,4,5,6,7,8,9,10 };
private final String[] barcodeT = { "001" , "002", "003", "004", "005",null,null,null,null,null, };
private final String[] nameT = { "Milk", "Bread", "Wine", "Butter", "Potato", "Sweet Potato","Ginger" ,"Avacado", "Cucumber"};
private final double[] perWeightT = {0.2,0.4,0.1,0.2,0.1,2,1,0.5,0,0,0};
private final double[] perPriceT = { 1.50, 2.45, 12.95, 4.75, 2, 3.5, 12, 4, 3, 1 };
private final ArrayList<ItemType> table = new ArrayList<>();
public final ArrayList<View> lookUpItem = new ArrayList<>();
public ItemTable()
{
for(int i = 0; i< idT.length; i++)
{
if(barcodeT[i]!=null)
table.add(new ItemTypeB (idT[i], nameT[i], perPriceT[i],perWeightT[i],barcodeT[i]));
else
{
if(perWeightT[i]!=0)
{
table.add( new ItemTypeW(idT[i], nameT[i], perPriceT[i],perWeightT[i],perWeightT[i]));
lookUpItem.add( new View(idT[i], nameT[i], "w"));
}
if(perWeightT[i]==0)
{
table.add( new ItemTypeC(idT[i], nameT[i], perPriceT[i],perWeightT[i], quantityT[i] ));
lookUpItem.add( new View(idT[i], nameT[i], "c"));
}
}
}
}
问题区域具体在最后一行代码附近的 table.add(new ItemTypeC)
。
您的一个数组 (nameT
) 是 "one-off";意思是:你所有的数组都有 10 个元素,但只有 9 个成员。
但实际问题的性质不同:您在这里做错了。您的程序是以 "procedural" 风格编写的。
含义:您有各种元素 "together" 形成有意义的东西。您的实施:将这些元素中的每一个放入其自己的数组中;并从每个数组中获取值 "build" 它们应该表示的意思 "together"。所以构成你的 "entity" 的东西只是 5 个不同数组中的索引。
在面向对象的方法中,您创建了抽象。像一个 class Product,它有多种属性;如"id"、"quantity"、"name"等。这也允许您创建合理的 equals 方法来轻松判断两个产品是否相同。当你创建了这样一个 class;然后创建 one 此类对象的数组。
长话短说:Java 中的编程是关于寻找和创建良好的抽象。如果你不这样做......你 运行 正是在这种低级问题中。
您的 nameT
数组只有 9 个元素。这会破坏您的代码。
当 i= 10 在 if(perWeightT[i]==0)
你的代码抛出 ArrayOutofBound 错误