我attemping填充的JComboBox與城市的名字。返回從一個HashMap的所有按鍵withou循環
我的程序有一個名爲「國家」級。 Country對象包含一個名爲'City'的對象的HashMap,其方法爲getName,返回一個String值。
public class Country {
private final Map<String, City> cities = new HashMap<>();
public Collection<City> getCities() {
return cities.values();
}
}
public class City {
String cityName;
public String getName() {
return cityName;
}
}
是否有可能不使用循環返回cityName的字符串數組?我嘗試以下,但它沒有工作:
Country country 1 = new Country();
String[] cityNames = country1.getCities().toArray();
JComboBox cityChoice = new JComboBox(cityNames);
這將返回市對象的數組,但我不知道如何與這個使用市GetName方法結合進行。
重複的http://stackoverflow.com/questions/3293946/the-easiest-way-to-transform-collection-to-array – skozlov 2015-04-04 11:20:33
不要認爲這是重複的 - 他們是清楚相關的,但這個問題是關於將特定類型的集合轉換爲包含原始集合的屬性而不是集合本身的數組。 – 2015-04-04 11:26:13
@ user3371750請閱讀[我應該怎麼做當有人回答我的問題](http://stackoverflow.com/help/someone-answers) – CKing 2017-01-28 07:26:01