我正在建造一个铁路模拟器,包括模拟器,车站,火车,路线和乘客。我将路由对象作为参数传递给火车对象时遇到了问题 - 它一直是空的。我已将我的代码作为解释问题的最佳方式。
import java.util.ArrayList;
public class Simulator {
int numStations;
Route route1;
ArrayList<Station> stations;
ArrayList<Train> trains;
Route[] routes;
public static void main(String[] args) {
//set up network
Simulator sim = new Simulator();
ArrayList<Station> stations = new ArrayList<Station>();
stations = sim.generateStations();
Route[] routes;
routes = sim.generateRoutes(stations);
System.out.println("Route array " + routes);
ArrayList<Train> trains = new ArrayList<Train>();
trains = sim.generateTrains(routes);
//start simulator
sim.generatePassengers(stations);
Train currentTrain = trains.get(0);
System.out.println("current train " + currentTrain);
//route null here
System.out.println("Route of current train " + currentTrain.route);
}
public Route[] generateRoutes(ArrayList<Station> stations){
//initialise routes between stations
Station[] stationList1 = {stations.get(0), stations.get(1), stations.get(2), stations.get(3), stations.get(4), stations.get(5)};
int[] stationDist1 = {200,100,200,300,200,300};
Route route1 = new Route(true, stationList1, stationDist1);
System.out.println("route1 " + route1);
//make list of all routes
Route[] routeList;
routeList = new Route[1];
routeList[0] = route1;
return routeList;
}
public ArrayList<Train> generateTrains(Route[] routes){
//initialise trains
ArrayList<Train> trainList = new ArrayList<Train>();
trainList.add(new Train(routes[0], 100, 0, true, 5));
trainList.add(new Train(routes[0], 100, 4, false, 10));
System.out.println("first train in list " + trainList.get(0));
System.out.println("first route in array " + routes[0]);
//route is null here
System.out.println("route of first train " + (trainList.get(0)).route);
return trainList;
}
}
这是我的输出:
route1 Route @ addbf1
路线阵列[LRoute; @ 42e816
阵列Route @ addbf1
中的Route1列车中的第一列火车@ 190d11
数组Route @ addbf1
中的第一个路由第一列火车的路线
目前的火车列车@ 190d11
当前列车的路线为空
有谁可以解释我哪里出错?
编辑: 火车类声明:
public class Train {
Route route;
int capacity;
int recentLoc;
boolean forwards;
boolean atStation;
int speed;
Train currentTrain;
int timeAtStation;
//train constructor
public Train(Route r, int c, int i, boolean f, int s){
r = route;
c = capacity;
i = recentLoc;
f = forwards;
s = speed;
atStation = true;
timeAtStation = 0;
}
答案 0 :(得分:2)
您没有为实例变量赋值。 object的默认值为null。在Train构造函数中将“r = route”更改为“route = r”(所有其他字段都相同)。
public class Train {
Route route;
int capacity;
int recentLoc;
boolean forwards;
boolean atStation;
int speed;
Train currentTrain;
int timeAtStation;
//train constructor
public Train(Route r, int c, int i, boolean f, int s){
route = r;
capacity = c;
recentLoc = i;
forwards = f;
speed = s;
atStation = true;
timeAtStation = 0;
}
答案 1 :(得分:1)
route = r;
capacity = c;
等
答案 2 :(得分:0)
您的输出中有三个问题。
首先,当你想要为数组打印conntent时,你必须遍历它的元素,否则你只需要打印内存地址。为了帮助你解决hava API的方法:
Arrays.toString(Object[]);
同样的问题在你的Train
中,你没有覆盖toString()
方法,这就是为什么Java打印它的地址,因为这是它的默认行为。
尝试将此方法添加到路由类中,看看会发生什么,
@Override
public String toString(){
return "This is message from rout"
}
Ant在火车的构造中最后一次
您将参数分配给变量。你应该反过来。
public Train(Route r, int c, int i, boolean f, int s){
this.route = r;
this.capacity = c;
this.recentLoc = i;
this.forwards = f;
this.speed = s;
this.atStation = true;
this.timeAtStation = 0;
}
答案 3 :(得分:0)
这些作业应该是相反的,左边的东西是分配的东西。
r = route;
c = capacity;
i = recentLoc;
f = forwards;
s = speed;
应该是
route = r;
capacity = c;
recentLoc = i;
forwards = f;
speed = s;
答案 4 :(得分:0)
你的作业错误。
r = route;
这意味着route
的值将分配给r
。
将构造函数更改为以下内容:
public Train(Route r, int c, int i, boolean f, int s)
{
route = r;;
capacity = c;;
recentLoc = i;
forwards = f;
speed = s;
atStation = true;
timeAtStation = 0;
}