我是 Java 新手。我想知道存储具有不同类型数据的二维数组的最佳选择是什么。
这将是国家表,每个国家都有首都并且位于大陆。然后我必须这样存储它:ContinentID | 国家名称 | 首都
选择什么?
您可能需要考虑创建一个Country类来保存此数据,然后维护此类实例的列表/数组。
public class Country {
private int continentId;
private String name;
private String capital;
public Country(int continentId, String name, String capital) {
this.continentId = continentId;
this.name = name;
this.capital = capital;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
然后你会得到类似的东西
List<Country> countries = new ArrayList<Country>();
countries.add(new Country(123, "USA", "Washington DC"));
...
Run Code Online (Sandbox Code Playgroud)