我有一个程序,我用一个数组列表来存放一些驾驶室物体。我一直得到一个错误,我从消息中得到的是,java不能识别数组列表中有对象。这是我得到的错误。Java不识别ArrayList中的元素?
异常在线程 “主” java.lang.IndexOutOfBoundsException:索引:20,尺寸:20
在java.util.ArrayList.rangeCheck(未知来源)
在java.util.ArrayList.get(未知源)
在edu.Tridenttech.MartiC.app.CabOrginazer.main(CabOrginazer.java:48)
这是我试图去上班代码
public class CabOrginazer {
private static List<CabProperties> cabs = new ArrayList<CabProperties>();
private static int count = 0;
private static boolean found = false;
public void cabOrginazer()
{
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CabRecordReaper reaper = new CabRecordReaper("C:/CabRecords/September.txt");
CabProperties cabNum;
for(int i = 0; i < 20; i++)
{
cabNum = new CabProperties();
cabs.add(cabNum);
}
while(reaper.hasMoreRecords())
{
CabRecord file = reaper.getNextRecord();
for(int j = 0; j < cabs.size(); j++)
{
if(cabs.get(j).getCabID() == file.getCabId())
{
found = true;
cabs.get(j).setTypeAndValue(file.getType(), file.getValue(), file.getPerGallonCost());
cabs.get(j).setDate(file.getDateString());
break;
}
}
if(found == false)
{
cabs.get(count).setCabId(file.getCabId());
count++;
}
/*for(CabProperties taxi : cabs)
{
if(taxi.getCabID() == file.getCabId())
{
found = true;
taxi.setTypeAndValue(file.getType(), file.getValue(), file.getPerGallonCost());
taxi.setDate(file.getDateString());
break;
}
}*/
}
for(CabProperties taxi : cabs)
{
System.out.print("cab ID: " + taxi.getCabID());
System.out.print("\tGross earning: " + taxi.getGrossEarn());
System.out.print("\tTotal Gas Cost: " + taxi.getGasCost());
System.out.print("\tTotal Service Cost: " + taxi.getServiceCost());
System.out.println();
}
}
}
line 48是if语句的内部,它表示cabs.get(count).setCabId(file.getCabId());
与我对Java的一点知识。 Java应该知道cabs
中有元素,我应该可以设置cab的id
。什么会导致Java不能识别ArrayList被填充?