此问题涉及如何将乘客类型的对象添加到位于4个站的Hashmap中的Hashmap中。我的TrainStation对象包含一个站号ID,旅客对象包含一个乘客信息,包括将要映射到的车站。
public class TrainStation extends Actor {
int id;
public int getId() {
return id;
}
public TrainStation(int id) {
this.setColor(Color.black);
this.id = id;
}
public static Map<Integer,Passenger> passengersDepart = new HashMap<>();
public class Passenger {
int id;
int appearTimeStep;
int originStationId;
int destinationStationsId;
int tripStartTimeStep;
int tripEndTimeStep;
int traveTime;
public Passenger(int id, int appearTimeStep, int originStationId, int destinationStationId) {
this.id = id;
this.appearTimeStep = appearTimeStep;
this.originStationId = originStationId;
this.destinationStationsId = destinationStationId;
}
4个站点被添加到主站点中,并存储在Dataset类文件中站点的单独静态映射中(未显示)。我从CSV中读取了乘客信息,并根据他们的车站字段对其进行了排序,并希望将其添加到相应的车站乘客地图中。
public static void readData(String path) throws IOException {
BufferedReader pBufferedReader = new BufferedReader(new FileReader(path));
String newline;
//read header, output index number of each item
String[] header = pBufferedReader.readLine().split(",");
int idIndex = findPositionInArray("id", header);
int appearTimeStepIndex = findPositionInArray("appearTimeStep", header);
int originStationId = findPositionInArray("originStationId", header);
int destinationStationsId = findPositionInArray("destinationStationId", header);
//read body lines
while ((newline = pBufferedReader.readLine()) != null) {
String[] lineElements = newline.split(",");
int id = Integer.parseInt(lineElements[idIndex]);
int tStep = Integer.parseInt(lineElements[appearTimeStepIndex]);
int oId = Integer.parseInt(lineElements[originStationId]);
int dId = Integer.parseInt(lineElements[destinationStationsId]);
Passenger passenger = new Passenger(id, tStep, oId, dId);
//I NEED THIS TO ACCESS EACH INDIVIDUAL STATION MAP
if (passenger.destinationStationsId == 1) {
DataSet.tsMap.put(passenger.getId(), passenger);
}
else if (passenger.destinationStationsId == 2){
TrainStation.passengersDepart.put(passenger.getId(), passenger);
}
else if (passenger.destinationStationsId == 3) {
TrainStation.passengersDepart.put(passenger.getId(), passenger);
}
else {
TrainStation.passengersDepart.put(passenger.getId(), passenger);
}
}
pBufferedReader.close();
}
我面临的挑战是将通过CSV创建的旅客对象放入火车站的哈希图中(无论如何它不会去,因为它是旅客而不是火车站类型),而是放在人员列表中火车站哈希图中的哈希图。