创建具有数组成员的类的实例

时间:2018-06-10 15:24:25

标签: java arrays object

我试图在没有运气的情况下创建这个对象的实例,我知道它因为数组但我怎样才能使它与数组一起工作?

public class City {
    private String Country_Name;
    private String [] Cities ;
    private String [] Destinations;}

    public City(String Country_Name, String [] Cities, String [] Destinations) {
        this.Country_Name = Country_Name;
        this.Cities = new String [2];
        this.Destinations = new String [2];
    }

    public class ASSIGNMENT {

    public static void main(String[] args) throws ParseException {
        LinkedList<FLIGHT> ob1 = new LinkedList<FLIGHT>();
        LinkedList<FLIGHT_BOOKING> ob = new LinkedList<FLIGHT_BOOKING>();
        LinkedList<City> ob3 = new LinkedList<City>();

        Scanner sc = new Scanner(System.in);
        Scanner ss = new Scanner(System.in);
        FLIGHT_BOOKING obj = new FLIGHT_BOOKING();
        FLIGHT obj1 = new FLIGHT();
        City c1 = new City("Malaysia","Kuala","Johor","melaka"},{"Cyberjaya","Putrajaya","Sunway"});
    }
}

1 个答案:

答案 0 :(得分:0)

你错了大括号,你在构造函数之前关闭了类定义。它应该如下:

public class City {
    private String Country_Name;
    private String [] Cities ;
    private String [] Destinations;

    public City(String Country_Name, String[] Cities, String[] Destinations) {
        this.Country_Name = Country_Name;
        this.Cities = new String [2];
        this.Destinations = new String [2];
    }
}

另外,你错过了打开花括号。按如下方式创建对象:

String[] cities = {"Kuala","Johor"}; // Giving only 2 cities as in constructor you've given as new String[2]
String[] destinations = {"Cyberjaya","Putrajaya"}; // Same as above
City c1 = new City("Malaysia", cities, destinations);

如果您想要更多城市和目的地,请将构造函数更新为

public City(String Country_Name, String[] Cities, String[] Destinations) {
            this.Country_Name = Country_Name;
            this.Cities = Cities;
            this.Destinations = Destinations;
        }