我正在尋找創建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 朋友謝謝你 ;)
uj5u.com熱心網友回復:
所以你想要一個帶有字串索引的 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 撰寫這個專案。
- 有關地圖的更多資訊
- 關于地圖的另一個例子(查看答案)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/413258.html
標籤:
