Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java无法识别ArrayList中的元素?_Java_Arraylist - Fatal编程技术网

Java无法识别ArrayList中的元素?

Java无法识别ArrayList中的元素?,java,arraylist,Java,Arraylist,我有一个程序,我制作了一个arraylist来保存一些cab对象。我不断得到一个错误,我从消息中得到的是java无法识别arraylist中有对象。这就是我得到的错误 线程“main”java.lang.IndexOutOfBoundsException中的异常:索引:20,大小:20 位于java.util.ArrayList.rangeCheck(未知源) 在java.util.ArrayList.get(未知源代码) 位于edu.tridentech.MartiC.app.CabOrgin

我有一个程序,我制作了一个arraylist来保存一些cab对象。我不断得到一个错误,我从消息中得到的是java无法识别arraylist中有对象。这就是我得到的错误

线程“main”java.lang.IndexOutOfBoundsException中的异常:索引:20,大小:20
位于java.util.ArrayList.rangeCheck(未知源)
在java.util.ArrayList.get(未知源代码)
位于edu.tridentech.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();

    }


}

}
public-class CabOrginazer{
私有静态列表cabs=new ArrayList();
私有静态整数计数=0;
找到私有静态布尔值=false;
公共无效cabOrginazer()
{
}
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
CabRecordReaper收割机=新的CabRecordReaper(“C:/CabRecords/Septer.txt”);
cabNum;
对于(int i=0;i<20;i++)
{
cabNum=新的CabProperties();
cabs.add(cabNum);
}
while(reaper.hasMoreRecords())
{
CabRecord文件=reaper.getNextRecord();
对于(int j=0;j
第48行是if语句的内部,它说
cabs.get(count).setCabId(file.getCabId())
我对Java知之甚少。Java应该知道
cab中有一些元素,我应该能够设置cab的
id
。什么会导致Java无法识别arraylist已填充

列表中的项目
count
处没有填充元素。看看例外情况:列表中有20个元素,因此有效索引为0到19(包括)。您要求的是第20条记录(即第21条记录)。那是不存在的

听起来你的街区应该是这样的:

if (!found)
{
    CabProperties properties = new CabProperties();
    properties.setCabId(file.getCabId());
    // Probably set more stuff
    cabs.add(properties);
}

您可能完全能够摆脱
count
变量,以及使用伪属性的列表的初始填充。首先填充这样一个列表是非常奇怪的——这通常是使用固定大小的数组所做的事情。使用
列表(如
ArrayList
)的主要好处之一是它的大小是动态的。

Java可以很好地识别成员。数组中有20个成员,从索引0到索引19进行索引

您要求的是索引20,它不存在

循环用于:

while(reaper.hasMoreRecords())
必须比您预期的运行次数多得多,并且您的数据多次遇到
found==false
if条件(您可以说
if(!found){…
),第21次失败时出现索引越界异常

您还应该了解如何使用调试器