我有一个航空公司字符串数组,我需要解析为飞机对象数组的属性。我已经创建了对象数组,我使用我的setAirline方法有问题,程序编译但似乎没有存储值,截至目前我只有setAirline来测试它是否有效。
public class Project2 {
// Input data - Airline, Flight Number, Make, Model, Departure City, Arrival City, Departure Time, Arrival Time
final static String [][] DATA = {
{"Air Berlin", "4504", "Boeing", "767-300", "Zurich (ZRH)", "New York, NY (JFK)", "2-26 1000", "2-26 1325"},
{"Qatar Airways", "212", "Boeing", "767-300", "Milano (MXP)", "New York, NY (JFK)", "2-26 1000", "2-26 1330"},
{"Jetblue Airways", "298", "Airbus", "A320", "Tampa, FL (TPA)", "New York, NY (JFK)", "2-26 0728", "2-26 0959"},
{"Air Canada", "6851", "Airbus", "A340-300", "Dakar (DKR)", "New York, NY (JFK)", "2-26 0255", "2-26 0650"},
{"Alaska Airlines", "1219", "Boeing", "737-800", "Boston, MA (BOS)", "New York, NY (JFK)","2-26 0940", "2-26 1055"},
{"Delta Airlines", "6182", "Canadadair", "CRJ", "Toronto (YYZ)", "New York, NY (JFK)", "2-26 0606", "2-26 0800"},
{"American Airline", "199", "Boeing", "767-300", "Milano (MXP)", "New York, NY (JFK)", "2-26 1010", "2-26 1330"},
{"United Airlines", "9855", "", "", "Cairo (CAI)", "New York, NY (JFK)", "2-26 1025", "2-26 0325"},
{"Virgin America", "483", "Airbus", "A320", "San Francisco (SFO)", "New York, NY (JFK)", "2-25 2320", "2-26 0750"},
{"Air France", "3650", "Boeing", "767-400", "Barcelona (BCN)", "New York, NY (JFK)", "2-26 1055", "2-26 0158"},
{"Air China", "7266", "Boeing", "757-200", "Los Angeles, CA (LAX)", "New York, NY (JFK)","2-25 2159", "2-26 0616"}
};
public static class Air{
private String airline, flightNumber, make, model, departure, arrival, departureTime, arrivalTime;
public String getAirline(Air a){return airline;};
public void setAirline(String n){airline = n;};
public String getFlightNum(){return flightNumber;};
public void setFlightNum(String n){flightNumber = n;};
public String getMake(){return make;};
public void setMake(String n){make = n;};
public String getModel(){return model;};
public void setModel(String n){model = n;};
public String getDep(){return departure;};
public void setDep(String n){departure = n;};
public String getArrival(){return arrival;};
public void setArrival(String n){arrival = n;};
public String getDepTime(){return departureTime;};
public void setDepTime(String n){departureTime = n;};
public String getArrivalTime(){return arrivalTime;};
public void setArrivalTime(String n){arrivalTime = n;};
public Air(){
make = "";
model = "";
}//default
public Air(String ma,String mo){
this.make=ma;
this.model=mo;
}//2 constructor
public Air(String air, String flight,String ma,String mo){
this.make=ma;
this.model=mo;
this.airline=air;
this.flightNumber=flight;
}//4 constructor
}//Air class
public static void main(String[] args) {
// Create an Array of airplanes
int size = DATA.length;
Air[] airplanes = new Air[size];
for(int i=0; i<size; i++){
for(int j=0;j<8;j++){
airplanes[i].setAirline(DATA[i][j]);
}//J-loop
}//I-loop
// Print out the original flight details
// Sort it
// Print out the updated flight details
}//main
}
答案 0 :(得分:0)
请问我,但我认为该程序在运行时给你一个NullPointerException
:
public static void main(String[] args) {
// Create an Array of airplanes
int size = DATA.length;
Air[] airplanes = new Air[size];
for (int i=0; i<size; i++) {
for (int j=0;j<8;j++) {
airplanes[i].setAirline(DATA[i][j]);
}//J-loop
}//I-loop
}//main
您没有初始化Air
数组中的airplane
个对象。代码airplanes[i].setAirline(DATA[i][j]);
如何在不给你例外的情况下运行?!
答案 1 :(得分:0)
你能解释一下,为什么你的Air.getAirline()需要Air-param?也许这是一个错字,你最好用这个解决方案结束:
public class AirplanTest {
final static String[][] DATA = {
{ "Air Berlin", "4504", "Boeing", "767-300", "Zurich (ZRH)",
"New York, NY (JFK)", "2-26 1000", "2-26 1325" },
{ "Qatar Airways", "212", "Boeing", "767-300", "Milano (MXP)",
"New York, NY (JFK)", "2-26 1000", "2-26 1330" }
};
public static class Air {
private String airline;
public String getAirline() {
return airline;
};
public void setAirline(String n) {
airline = n;
};
public Air() {
}// default
}// Air class
public static void main(String[] args) {
List<Air> airlines = new ArrayList<Air>();
for (int i = 0; i < DATA.length; i++) {
Air airplane = new Air();
for (int j = 0; j < 8; j++) {
airplane.setAirline(DATA[i][0]);
}// J-loop
airlines.add(airplane);
}// I-loop
// Print out the original flight details
for(int i=0; i<airlines.size();i++){
System.out.println(airlines.get(i).getAirline());
}
// Sort it
// Print out the updated flight details
}// main
}