Java doesn't recognize elements in ArrayList?

I have a program where I make an arraylist to hold some booth objects. I keep getting the message that I get the message from the message that java doesn't recognize that the arraylist has objects. This is the error I am getting.

Exception on stream "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)

This is the code I am trying to execute

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 is the inside of that if statement, where stated cabs.get(count).setCabId(file.getCabId());

with a little knowledge I have in Java. Java needs to know there are elements inside cabs

and I need to be able to install the id

cabins. What can cause Java not to know that the array is inhabited?

+3


source to share


2 answers


The list is not populated with an item in an item count

. Look at the exception: you have 20 elements in the list, so valid indices are 0 through 19 inclusive. You are requesting record 20 (i.e. the 21st record). It doesn't exist.

It seems that your block should look something like this:



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

      

You might as well get rid of the variable entirely count

- and your initial collection of list with dummy properties. It is very strange to fill in a list like this to start with - which you usually do with a fixed-size array. One of the main advantages of using List

it like ArrayList

is that it is dynamically sized.

+7


source


Java recognizes members just fine. You have 20 members in your array, indexed from index 0 to index 19.

You are asking for index 20, which does not exist.

Cycle for:



while(reaper.hasMoreRecords())

      

there has to be a lot more execution than you expect, and your data is beaten by the found == false

if condition (which you can just say if (!found) { ...

) many times, and 21 times it fails with index-out- exceptions.

You should also figure out how to use your debugger.

+4


source







All Articles