加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java无法识别ArrayList中的元素?

发布时间:2020-12-15 04:44:16 所属栏目:Java 来源:网络整理
导读:我有一个程序,我在那里制作一个arraylist来保存一些cab对象.我一直得到一个错误,我从消息中得到的是 java不能识别arraylist中有对象.这是我得到的错误. Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 20,Size: 20 at java.util
我有一个程序,我在那里制作一个arraylist来保存一些cab对象.我一直得到一个错误,我从消息中得到的是 java不能识别arraylist中有对象.这是我得到的错误.

Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 20,Size: 20
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at 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.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();

    }


}

}

第48行是if语句的内部,它表示cabs.get(count).setCabId(file.getCabId());
我对Java的了解很少. Java应该知道cab中有元素,我应该能够设置cab的id.是什么导致Java无法识别arraylist是否已填充?

解决方法

列表中没有填充项目计数的元素.查看异常:列表中有20个元素,因此有效索引为0到19(含).你要求记录20(即第21记录).那不存在.

这听起来像你的块应该是这样的:

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

您很可能完全摆脱count变量 – 以及具有虚拟属性的列表的初始填充.填充这样的列表是非常奇怪的 – 这通常是你用一个固定大小的数组做的事情.使用List(例如ArrayList)的主要好处之一是它的动态大小.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读