我希望在Java中實現像這樣的例子定製索引矩陣:我可以自定義Java中矩陣的索引嗎?
country1 city1 name1 region1 population1
country2 23 5 55 ...
city2 5 9 .
name2 .
region2 .
population2
也就是說mat[country1][country2]
應該回到23 我不知道我怎麼會做到這一點。
我希望在Java中實現像這樣的例子定製索引矩陣:我可以自定義Java中矩陣的索引嗎?
country1 city1 name1 region1 population1
country2 23 5 55 ...
city2 5 9 .
name2 .
region2 .
population2
也就是說mat[country1][country2]
應該回到23 我不知道我怎麼會做到這一點。
你很可能不得不使用哈希映射或哈希表具有關鍵作用就像對包含這兩個指標:Map<Pair<K1,K2>, V>
你可以看一看,對於更多的信息: Map with two-dimensional key in java
如果您有兩維表,其行和列總是遵循這個順序,那麼你可以使用行/列的枚舉。
例如:
public static final int COUNTRY = 0;
public static final int CITY = 1;
public static final int NAME = 2;
public static final int REGION = 3;
public static final int POPULATION = 4;
在另一方面,如果你能在數據結構中的多個國家,在這兩個方面,那麼你可以使用地圖。