[SOLVED] 3D array of Strings calling handles/hooks

Issue

I’m looking to create a 3D array of String types. I’m exploring creative ideas on getting a paragraph of text into each 3D array cell.

Here’s a basic 3D array for visual representation:

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]);
                }
            }
        }
    }
}

This gives:

[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]

I need to figure out the best way to insert text in each cell, while still retaining a handle to keep track of which cell of text is being used.

So cells [0][0][0] would have something in them like

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

and cells [0][1][0] would have in each cell something like

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

I’d like to stay with a 3D array since it’s easier to manage. I can’t figure out how to get a handle to call on a cell without making the array bigger.

My DnD buddies thank you 😉

Solution

So you want a 3D array with string indexes. there is no such thing as far as i know, but you can use some other methods that uses Map. Map can store element for every type of elements, for example:

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

the first method is to separate with some character you wont use:

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
}

another way would use a map for the indexes:

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
}

The third way would use 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
}

Note that every example is different and maybe one will fit your purpose and one not. its much simpler in javascript and your program seems to fit much more to javascript, so consider writing this project using javascript if you can.

Answered By – Ofek

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *