I have a method createCountry() and a method printAllCountries, which iterates through all countries and prints each of them if they're not null. The problem I have, is that I was only able to print out the name of one country by writing return countries[0]. However, I'd like to print all countries. Can someone therefore tell me what I need to change/consider?
...
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()?