线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:9

时间:2016-08-14 05:55:10

标签: java arrays for-loop inheritance multiple-inheritance

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)附近。

1 个答案:

答案 0 :(得分:1)

您的nameT数组只有9个元素。这会打破你的代码。

当{= 1}时i = 10时,您的代码会抛出ArrayOutofBound错误