所以你想要一个带有字符串索引的 3D 数组。据我所知,没有这样的事情,但是您可以使用其他一些使用 Map 的方法。 Map 可以存储每种元素的元素,例如:
Map<String,String> map = new HashMap<>();
map.put("abc", "def");
map.put("123", "456");
System.out.println(map.get("abc"));//prints def
System.out.println(map.get("123"));//prints 456
System.out.println(map.get("key"));//prints null
第一种方法是用一些你不会使用的字符来分隔:
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
char sep = ';'; //separator
map.put("Chapter 1: Grizzly" + sep + "Scene: Forest" + sep + "Time/Date: December 0100 hours", "win");
map.put("Chapter 1: Grizzly" + sep + "Scene: Gondor" + sep + "Time/Date: December 0100 hours", "lose");
String result = map.get("Chapter 1: Grizzly" + sep + "Scene: Forest" + sep + "Time/Date: December 0100 hours");
System.out.println(result);//win
}
另一种方法是使用地图作为索引:
public static void main(String[] args) {
Map<String,Integer> chapter = new HashMap<>();
Map<String,Integer> scene = new HashMap<>();
Map<String,Integer> time = new HashMap<>();
String[][][] arr = new String[3][3][3];
//setup the maps:
chapter.put("Chapter 1: Grizzly", 0);
chapter.put("Chapter 2: Contour", 1);
chapter.put("Chapter 3: Dragon", 2);
scene.put("Scene: Forest", 0);
scene.put("Scene: Gondor", 1);
scene.put("Scene: Desert", 2);
time.put("Time/Date: December 0100 hours", 0);
time.put("Time/Date: January 0100 hours", 1);
time.put("Time/Date: February 0100 hours", 2);
//now we can use it:
arr[chapter.get("Chapter 1: Grizzly")][scene.get("Scene: Forest")][time.get("Time/Date: December 0100 hours")] = "win";
arr[chapter.get("Chapter 1: Grizzly")][scene.get("Scene: Gondor")][time.get("Time/Date: December 0100 hours")] = "lose";
String result = arr[chapter.get("Chapter 1: Grizzly")][scene.get("Scene: Forest")][time.get("Time/Date: December 0100 hours")];
System.out.println(result);//win
}
第三种方式是使用Map of Map的Map:
public static void main(String[] args) {
Map<String,Map<String,Map<String,String>>> map = new HashMap<>();
//let build the map
//first let build the Forest scene
Map<String,String> forestScene = new HashMap<>();
forestScene.put("Time/Date: December 0100 hours", "win");
forestScene.put("Time/Date: January 0100 hours", "win");
forestScene.put("Time/Date: February 0100 hours", "lose");
//now the Gondor scene
Map<String,String> gondorScene = new HashMap<>();
gondorScene.put("Time/Date: December 0100 hours", "win");
gondorScene.put("Time/Date: January 0100 hours", "win");
gondorScene.put("Time/Date: February 0100 hours", "lose");
//...
//let put all the scenes inside a map for :
Map<String,Map<String,String>> chapter1 = new HashMap<>();
map.put("Scene: Forest", forestScene);
map.put("Scene: Gondor", gondorScene);
//...
//we need to build every chapter and then we can add them all to a main map:
Map<String,Map<String,Map<String,String>> map = new HashMap<>();
map.put("Chapter 1: Grizzly", chapter1);
//map.put("Chapter 2: Contour", chapter2);
//map.put("Chapter 3: Dragon", chapter3);
//now we can use this:
String result = map.get("Chapter 1: Grizzly").get("Scene: Forest").get("Time/Date: December 0100 hours");
System.out.println(result);//win
}
请注意,每个示例都是不同的,也许一个适合您的目的,而一个不适合。它在 javascript 中要简单得多,而且您的程序似乎更适合 javascript,所以如果可以的话,请考虑使用 javascript 编写这个项目。