包含其他枚举数组的Java Enum实例(Continent,Country):返回Array中的空值

时间:2011-03-03 21:12:28

标签: java arrays enums initialization

当我调用Enum Continent.EUROPE的getCountries()时,我得到一个正确大小的列表,但是包含的值(应该是Country类型的枚举)都是“null”。 我做初始化错了吗?

我的枚举:

public enum Continent {
    ANY, AFRICA, ANTARCTICA, ASIA, AUSTRALIA_OCEANIA, EUROPE, NORTH_AMERICA, SOUTH_AMERICA;

    private Country[] countries;

    public void setCountries(Country[] countries) {
    this.countries = countries;
    }

    public Country[] getCountries() {
    return this.countries;
    }

    static {
    EUROPE.setCountries(new Country[] { Country.ALAND_ISLANDS,
        Country.ALBANIA, Country.ANDORRA, Country.AUSTRIA,
        Country.BELARUS, Country.BELGIUM, Country.BOSNIA_HERZEGOVINA,
        Country.BULGARIA, Country.CROATIA, Country.CZECH_REPUBLIC,
        Country.DENMARK, Country.ESTONIA, Country.FAROE_ISLANDS,
        Country.FINLAND, Country.FRANCE, Country.GERMANY,
        Country.GIBRALTAR, Country.GREECE, Country.GUERNSEY,
        Country.HUNGARY, Country.ICELAND, Country.IRELAND,
        Country.ISLE_OF_MAN, Country.ITALY, Country.JERSEY,
        Country.KOSOVO, Country.LATVIA, Country.LIECHTENSTEIN,
        Country.LITHUANIA, Country.LUXEMBOURG, Country.MACEDONIA,
        Country.MALTA, Country.MOLDOVA, Country.MONACO,
        Country.MONTENEGRO, Country.NETHERLANDS, Country.NORWAY,
        Country.POLAND, Country.PORTUGAL, Country.ROMANIA,
        Country.RUSSIAN_FEDERATION, Country.SAN_MARINO, Country.SERBIA,
        Country.SLOVAKIA, Country.SLOVENIA, Country.SPAIN,
        Country.SVALBARD_AND_JAN_MAYEN, Country.SWEDEN,
        Country.SWITZERLAND, Country.UKRAINE, Country.UNITED_KINGDOM,
        Country.VATICAN_CITY });
    }
}

My Country Enum:

public enum Country {
    ALAND_ISLANDS("AX", Continent.EUROPE, new City[] {} ),
    ALBANIA("AL", Continent.EUROPE, new City[] {} ),
    ANDORRA("AD", Continent.EUROPE, new City[] {} ),
    ...
    private Continent continent;
    private String countryCode;
    private City[] cities;

    private Country(String countryCode, Continent continent, City[] cities ) {
        this.continent = continent;
        this.countryCode = countryCode;
        this.cities = cities;
    }

    public Continent getContinent() {
        return this.continent;
    }

    public City[] getCities() {
        return this.cities;
    }

    /**
     * @return the country code
     */
    public String getCountryCode() {
        return countryCode;
    }
}

Continent.EUROPE.getCountries() returns (in Eclipse debug view):
[null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]

正确的国家/地区数量,但都为空; - (

任何提示?

0 个答案:

没有答案