클래스 프로젝트를위한 엘리베이터 시뮬레이터 프로그램을 만들려고합니다. 나는 util.Timer를 만들어서 프로그램을 테스트하려고 노력했다. 무작위 바닥에 사람들을 태우고 엘리베이터를 데려다 줬지만, MyTimerTask 클래스의 completeTask() 메소드에 대한 코드를 작성할 때 내 승객을 저장 한 arraylist는 그 사이즈를 0으로 설정하면 (자), IndexOutOfBounds 예외가 발생합니다. 왜 이런 일이 일어나는지 모르겠습니다.timertask arraylist 크기를 0으로 설정 하시겠습니까?
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;
}
그리고 마지막으로, 코드의 나머지 부분이 나있을 수도 있고 없을 수도 있습니다 : 이제
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가의 내부 클래스는 엘리베이터의 코드가있다 : 첫째 MyTimerTask의 코드는 내 문제와 관련이있다. 왜냐하면 나는 그것을 일으키는 원인을 알지 못하기 때문이다.
//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();
}
이 코드가 다소 복잡해 보일 경우 죄송합니다. 코딩하는 것이 좋지 않습니다. 나를 도와 주셔서 미리 감사드립니다.
에서 참조하시기 바랍니다? 프로그램이 실행될 때 중요한 값을보기 위해 디버거를 사용하여 프로그램을 실행 했습니까? 전체 프로그램을 보여주기보다는 복사본을 가지고 동일한 문제를 보여주는 최소한의 프로그램으로 축소 해보십시오. – AdrianHHH