Question

I am trying to create an elevator simulator program for a class project. I tried to test my program by creating a util.Timer to create people in random floors for the elevator to pick them up, but when I wrote the code for the completeTask() method in the MyTimerTask class, the arraylist that stored my passengers had its size set to 0 and then an IndexOutOfBounds Exception occurs. I don't know why this is happening. First is the code for MyTimerTask:

private class MyTimerTask extends TimerTask
{
    @Override
    public void run()
    {
        System.out.println("task");
        completeTask();
    }

    private void completeTask()
    {
        try
        {
            System.out.println(floors.size());
            floors.ensureCapacity(numFloors); // I tried to force the array to have a certain size but that didn't work, it got resest to 0.
            System.out.println(floors.size());
            int randFloor = (int)(Math.random()*6);
            floors.get(randFloor).addLast(new Passenger(randFloor,counter));

            loadPassenger(currentFloor);
            System.out.println(this.toString());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            counter++;
            if(counter >= 5)
                cancel();
        }
    }
}

Now is the code for Elevator, which MyTimerTask is an inner class of:

public class Elevator
{
    private int currentFloor;
    private static final int numFloors = 6;
    private static final int maxCapacity = 10;
    private LinkedList<Passenger> queue = new LinkedList<Passenger>();
    private ArrayList<LinkedList<Passenger>> floors;
    static int counter = 0;
    TimerTask task;
    Timer time;

    //MyTimerTask here

    public Elevator()
    {
        currentFloor = 1;
        floors = new ArrayList<LinkedList<Passenger>>(numFloors);
        for (LinkedList<Passenger> e: floors)     //here I thought that 
        if(e == null)                         //maybe the array needed to be
            e = new LinkedList<Passenger>();  //filled, so I tried that.

        time = new Timer(false);
        task = new MyTimerTask();
    }
    public TimerTask getTask()
    {
        return task;
    }
    public Timer getTimer()
    {
    return time;
    }

and finally, the rest of the code that may or may not have to do with my problem, since I don't know what is causing it.

//all part of the Elevator class
public void loadPassenger(int floor)
{
    int direction = queue.peekFirst().getDestination() - currentFloor;
    for(Passenger p: floors.get(currentFloor))
        if(direction > 0)
            if(p.getDestination() > currentFloor)
                queue.addLast(p);
        else
            if(p.getDestination() < currentFloor)
                queue.addLast(p);
    sortPassengers();

}

public void requestMove(int floor)
{
    currentFloor = floor;
}

public void moveAndUnload()
{
    currentFloor = queue.poll().getDestination();
    while(queue.getFirst().getDestination() == currentFloor)
        queue.poll();
}
public int getFloor()
{
    return currentFloor;
}

public void sortPassengers()
{
    int direction = queue.peekFirst().getDestination() - currentFloor;
    ArrayList<Passenger> up = new ArrayList<Passenger>();
    ArrayList<Passenger> down = new ArrayList<Passenger>();

    for(int k = 0; k < queue.size(); k++)
    {
        if(queue.get(k).getDestination() > currentFloor)
            up.add(queue.get(k));
        else
            down.add(queue.get(k));
    }

    //sorts up array
    for(int j = 0; j < up.size() - 1; j++)
    {
        Passenger min = up.get(j);
        for(int k = j; k < up.size() - 1; k++)
            if(up.get(k+1).getDestination() - currentFloor < min.getDestination() - currentFloor)
                min = up.get(k+1);

        if(min != up.get(j))
            up = swap(j, up.indexOf(min), up);
    }

    //sorts down array
    for(int j = 0; j < down.size()-1; j++)
    {
        Passenger min = down.get(j);
        for(int k = j; k < down.size() - 1; k++)
            if(currentFloor - down.get(k+1).getDestination() < currentFloor - min.getDestination())
                min = down.get(k+1);

        if(min != down.get(j))
            down = swap(j, down.indexOf(min), down);
    }

    if(direction > 0)
    {
        queue.addAll(up);
        queue.addAll(down);
    }
    else
    {
        queue.addAll(down);
        queue.addAll(up);
    }
}

private ArrayList<Passenger> swap(int first, int second, ArrayList<Passenger> p)
{
    Passenger temp = p.get(first);
    p.set(first, p.get(second));
    p.set(second, temp);
    return p;
}

public String toString()
{
    return queue.toString();
}

I'm sorry if this code looks rather messy, I'm kind of bad at coding. Thanks in advance for helping me out.

Was it helpful?

Solution

As per your code

System.out.println(floors.size());
floors.ensureCapacity(numFloors); 
// I tried to force the array to have a certain size but that didn't work
System.out.println(floors.size());

both SOPs will print same value.

  • capacity doesn't force an ArrayList to have a certain size. It will increment as soon as it is filled.

  • capacity just tells about the memory allocated for initial storage.

  • capacity doesn't change the size.


Please have a look at java arraylist ensureCapacity not working

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top