【问题标题】:3D array of Strings calling handles/hooks调用句柄/钩子的 3D 字符串数组
【发布时间】:2022-01-15 17:11:39
【问题描述】:

我希望创建一个 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 好友谢谢你 ;)

【问题讨论】:

    标签: java arrays performance multidimensional-array


    【解决方案1】:

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

    【讨论】:

    • 这太好了,谢谢。这些是一些好主意。我也很欣赏 javascript 评论。该算法将进入安卓应用程序。如果您对 Android 非常熟悉,我也会很感激该领域的反馈。我只是以前没有遇到过这样的概念。我会检查你提到的 Map 类型。再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 2019-06-16
    • 2022-11-24
    • 1970-01-01
    • 2021-08-28
    相关资源
    最近更新 更多