Java does not recognize elements in ArrayList?

I have a program where I make an ArrayList to save some cab objects I always get an error. What I get from the message is that Java does not recognize objects in ArrayList This is my mistake

This is the code I'm trying to get started

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();

    }


}

}

Line 48 is the interior of the if statement, which represents cabs get(count). setCabId(file.getCabId()); I know little about Java Java should know that there are elements in the cab. I should be able to set the ID of the cab. What makes Java unable to recognize whether the ArrayList has been filled?

Solution

There are no elements in the list to fill in the item count View exception: there are 20 elements in the list, so the valid indexes are 0 to 19 (inclusive) You asked for record 20 (i.e. record 21) That doesn't exist

It sounds like your block should be like this:

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

You are likely to get rid of the count variable – and the initial population of lists with virtual attributes It's very strange to fill in such a list - this is usually what you do with a fixed size array One of the main benefits of using lists (such as ArrayList) is its dynamic size

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>