timertask正在将数组列表大小设置为0



我正试图为一个类项目创建一个电梯模拟器程序。我试图通过创建一个util来测试我的程序。计时器,用于在随机楼层创建人员以便电梯接他们,但当我在MyTimerTask类中为completeTask()方法编写代码时,存储乘客的数组列表的大小设置为0,然后发生IndexOutOfBounds异常。我不知道为什么会发生这种事。首先是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();
        }
    }
}

现在是Elevator的代码,MyTimerTask是的内部类

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;
    }

最后,剩下的代码可能与我的问题有关,也可能与我无关,因为我不知道是什么原因造成的。

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

如果这个代码看起来很乱,我很抱歉,我有点不擅长编码。事先谢谢你帮我。

根据您的代码

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

两个SOP将打印相同的值。

  • 容量不会强制CCD_ 1具有特定的大小。一旦填满,它就会增加。

  • 容量只是告诉分配给初始存储的内存。

  • 容量不会改变大小。


请查看java arraylist ensureCapacity不工作

相关内容

  • 没有找到相关文章

最新更新