【发布时间】:2017-09-20 12:03:21
【问题描述】:
我有一个方法createCountry() 和一个方法printAllCountries,它遍历所有国家并打印它们,如果它们不是null。我遇到的问题是,我只能通过写return countries[0] 打印出一个国家的名称。但是,我想打印所有国家。因此,有人可以告诉我我需要改变/考虑什么吗?
...
public Country createCountry() {
Country[] countries = new Country[5];
Country Sweden = new Country ("Sweden", 498000000000l,10000000);
Country Brazil = new Country("Brazil", 1798000000000l, 208000000);
Country Germany = new Country("Germany", 38100000000000l, 826700000);
Country France = new Country("France", 24650000000000l, 66900000);
Country Italy = new Country("Italy", 18500000000000l, 60600000);
countries[0] = Sweden;
countries[1] = Brazil;
countries[2] = Germany;
countries[3] = France;
countries[4] = Italy;
return countries; // countries[0] etc. would work..
}
public void printAllCountries() {
for (int i = 0; i < countries.length; i++){
if (countries[i] != null ){
System.out.println(countries[i].getName());
}
}
}
...
【问题讨论】:
-
public Country createCountry()->public Country[] createCountry()?
标签: java