Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java timertask是否将arraylist大小设置为0?_Java_Arraylist_Linked List_Timertask - Fatal编程技术网

Java timertask是否将arraylist大小设置为0?

Java timertask是否将arraylist大小设置为0?,java,arraylist,linked-list,timertask,Java,Arraylist,Linked List,Timertask,我正在尝试为一个班级项目创建一个电梯模拟器程序。我试图通过创建一个util.Timer来测试我的程序,以便在随机楼层中创建人员供电梯接送,但当我在MyTimerTask类中为completeTask()方法编写代码时,存储乘客的arraylist的大小设置为0,然后发生IndexOutOfBounds异常。我不知道为什么会这样。首先是MyTimerTask的代码: private class MyTimerTask extends TimerTask { @Override pu

我正在尝试为一个班级项目创建一个电梯模拟器程序。我试图通过创建一个util.Timer来测试我的程序,以便在随机楼层中创建人员供电梯接送,但当我在MyTimerTask类中为completeTask()方法编写代码时,存储乘客的arraylist的大小设置为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();
        }
    }
}
现在是电梯的代码,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;
    }
公共级电梯
{
私人楼层;
私有静态最终整数楼层=6;
专用静态最终int最大容量=10;
私有LinkedList队列=新建LinkedList();
私人ArrayList楼层;
静态整数计数器=0;
时间任务;
定时器时间;
//这里是MyTimerTask
公共电梯()
{
当前楼层=1;
楼层=新阵列列表(numFloors);
for(LinkedList e:floors)//这里我认为
if(e==null)//可能需要
e=newLinkedList();//已填充,所以我尝试了。
时间=新计时器(错误);
任务=新的MyTimerTask();
}
公共时间任务getTask()
{
返回任务;
}
公共计时器getTimer()
{
返回时间;
}
最后,代码的其余部分可能与我的问题有关,也可能与我的问题无关,因为我不知道是什么导致了这个问题

//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();
}
//电梯类的所有部分
公共乘客(内楼层)
{
int direction=queue.peekFirst().getDestination()-currentFloor;
用于(乘客p:楼层。获取(当前楼层))
如果(方向>0)
如果(p.getDestination()>currentFloor)
queue.addLast(p);
其他的
如果(p.getDestination()currentFloor)
up.add(queue.get(k));
其他的
down.add(queue.get(k));
}
//整理数组
对于(int j=0;j0)
{
queue.addAll(向上);
queue.addAll(向下);
}
其他的
{
queue.addAll(向下);
queue.addAll(向上);
}
}
专用ArrayList交换(int first,int second,ArrayList p)
{
乘客温度=p.get(第一);
p、 set(第一,p.get(第二));
p、 设置(第二,温度);
返回p;
}
公共字符串toString()
{
返回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将打印相同的值

  • 容量不会强制
    ArrayList
    具有特定的大小。它会在填充后立即增加

  • 容量只是说明为初始存储分配的内存

  • 容量不会改变大小



请看一看

您做了什么来尝试和理解问题?您是否使用调试器运行程序以在程序执行时查看重要值?而不是显示整个程序尝试使用它的副本并将其减少到显示相同问题的最小程序。