调用 handles/hooks 的 3D 字符串数组

3D array of Strings calling handles/hooks

我想创建一个 String 类型的 3D 数组。我正在探索将一段文本放入每个 3D 数组单元格的创意。

这是用于视觉表示的基本 3D 数组:

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

        int[][][] array = new int[3][3][3];

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                for (int k = 0; k < array[i][j].length; k++) {
                    System.out.println("[" + i + "][" + j + "][" + k + "]:" + array[i][j][k]);
                }
            }
        }
    }
}

这给出:

[0][0][0] ; [0][1][0] ; [0][2][0]
[0][0][1] ; [0][1][1] ; [0][2][1]
[0][0][2] ; [0][1][2] ; [0][2][2]

[1][0][0] ; [1][1][0] ; [1][2][0]
[1][0][1] ; [1][1][1] ; [1][2][1]
[1][0][2] ; [1][1][2] ; [1][2][2]

[2][0][0] ; [2][1][0] ; [2][2][0]
[2][0][1] ; [2][1][1] ; [2][2][1]
[2][0][2] ; [2][1][2] ; [2][2][2]

我需要找出在每个单元格中插入文本的最佳方式,同时仍然保留一个句柄来跟踪正在使用哪个文本单元格。

所以单元格 [0][0][0] 中会有类似

的东西
["Chapter 1: Grizzly"]["Scene: Forest"]["Time/Date: December 0100 hours"]

和单元格 [0][1][0] 会在每个单元格中有类似

的内容
["Chapter 1: Grizzly"]["Scene: Gondor"]["Time/Date: December 0100 hours"]

我想继续使用 3D 阵列,因为它更易于管理。我不知道如何在不使数组变大的情况下获取调用单元格的句柄。

我的 DnD 小伙伴们谢谢你们 ;)

所以你想要一个带有字符串索引的 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 of 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 编写此项目。