Java 2D 列表,包含 3 列(int、String、String)

Krz*_*iak 2 java 2d list

我是 Java 新手。我想知道存储具有不同类型数据的二维数组的最佳选择是什么。

这将是国家表,每个国家都有首都并且位于大陆。然后我必须这样存储它:ContinentID | 国家名称 | 首都

选择什么?

ars*_*jii 5

您可能需要考虑创建一个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)