Ich habe eine HashMap und ich möchte alle Schlüsselwerte und die Attribute der Klassen drucken. Meine Klasse ist:HashMap <String, Klasse>
class Country {
String country;
int population;
Country(String country, int population) {
this.country=country;
this.population=population;
}
}
ich Hashmap erstellen möchten, wo Integer wird das Jahr zum Beispiel 2010 bis 2014, und die Klasse Land alle für dieses Jahr die Länder und Bevölkerung sein wird, und drucken später, dass HashMap, nach Jahr und retreiving den Namen und die Bevölkerung des Landes.
int year=1960;
HashMap<Integer,Country> country =new HashMap<Integer, Country>();
for (int j=0; j<10; j++)
{
String country=countrylist[j]);
int population=populationlist[j]);
Country P=new Country(country,population);
country.put(year, P);
year++;
}
for (Map.Entry p : country.entrySet()) {
Country country=(Country)p.getValue();
year=(Integer)p.getKey();
nameCountry=country.country;
population=country.population;
println(year,namecountry,population);
}
ok. was ist die Frage? Gibt es einen bestimmten Teil, wo du stecken geblieben bist? –
Ich bekomme nur die letzte Klasse, die ich eingeführt habe, also das letzte Land, wenn ich versuche zu drucken. –
int Jahr = 2010; Land P = neu (countryName, Population); Land.put (Jahr, P); –