我是Iterators的新手,不知道我做错了什么。在我的项目中,车站有汽车,有乘客,汽车也有乘客。我的目标是检查汽车是否已经到达目标站,如果没有,则将其从当前站移除并将其添加到下一站,将其移动到下一站。
Iterator<Station> stations = allStations.iterator();
while(stations.hasNext())
{
Station currentStation = (Station)stations.next();
ArrayList<Car> currentStationCars = currentStation.getCarList();
Iterator cars = currentStationCars.iterator();
Car currentCar = (Car)cars.next();
while(cars.hasNext())
{
最初,我在这里声明了currentCar,但是这导致了NoSuchElement异常 - 我猜是因为我在每次迭代时都继续向前移动光标。不确定,我一小时前才刚刚了解到这一点。现在,这段代码导致无限循环。
//original position of currentCar declaration
int stepper = 0;
if(currentCar.getCurrentLocation() < currentCar.getDestination())
{
stepper = 1;
}
else if(currentCar.getCurrentLocation() > currentCar.getDestination())
{
stepper = -1;
}
while(stepper != 0)
{
currentCar.setCurrentLocation(currentCar.getCurrentLocation() + stepper);
currentStation.removeCar(currentCar);
if(currentCar.getCurrentLocation() < currentCar.getDestination())
{
stepper = 1;
}
else if(currentCar.getCurrentLocation() > currentCar.getDestination())
{
stepper = -1;
}
else {
stepper = 0;
}
}
}
答案 0 :(得分:0)
你只是在循环之前推进迭代器一次。这意味着如果列表为空(例如,在检查cars.next()
之前调用cars.hasNext()
),并且如果它至少有一个元素则调用Car
,您将获得异常,因为您没有& #39; t前进到循环中的下一个while (cars.hasNext())
{
Car currentCar = (Car)cars.next();
....
}
。
你应该只在循环内推进迭代器:
Iterator<Car> cars = currentStationCars.iterator();
...
while(cars.hasNext())
{
Car currentCar = cars.next();
....
}
请注意,如果使用泛型类型,则可以避免转换:
Exception in thread "main" java.lang.NoClassDefFoundError:
"com/google/protobuf/MessageOrBuilder
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at AddPerson.main(AddPerson.java:69)
Caused by: java.lang.ClassNotFoundException:
com.google.protobuf.MessageOrBuilder
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 25 more"