文字列をコード化することができます。たとえば、次のように
Map<String, Integer> codec = new HashMap<String, Integer>() {{
put("a", 0);
put("b", 1);
put("c", 2);
}};
int adjMatrix[][] = new int[][]{
{0, 1, 0},
{0, 0, 1},
{0, 0, 0}
};
System.out.println(adjMatrix[codec.get("a")][codec.get("b")]); //prints "1"
それとも、Map
の使用しようとすることができます。
Map<String, Map<String, Integer>> adjMatrix = new HashMap<>();
Map<String, Integer> aRow = new HashMap<>();
aRow.put("a", 0);
aRow.put("b", 1);
aRow.put("c", 0);
Map<String, Integer> bRow = new HashMap<>();
bRow.put("a", 0);
bRow.put("b", 0);
bRow.put("c", 1);
Map<String, Integer> cRow = new HashMap<>();
cRow.put("a", 0);
cRow.put("b", 0);
cRow.put("c", 0);
adjMatrix.put("a", aRow);
adjMatrix.put("b", bRow);
adjMatrix.put("c", cRow);
System.out.println(adjMatrix.get("a").get("b")); // prints "1"
を