2014-05-13 124 views
5

我有一個數組,看起來像這樣爪哇3D陣列賦值

static String[][][] School= new String[1000][20][5]; 
  • 在第一托架我保存類名
  • 在第二我保存一個學生的ID
  • 在第三節中,我保存了關於學生的信息(他的名字,姓氏等)。

首先我分配所有的類名,之後我給每個類分配它的學生ID,然後我可以填寫他們的信息。

我該怎麼辦?我試過它與例如

School[i] = "A1"; 

但它不工作。

編輯:或者是否有其他方法來保存這3件事? (班級名稱,學生及其形式)

+0

選中此項 - [Java程序添加兩個三維(3D)數組。](http://www.msccomputerscience.com/2013/02/java-program -to-附加兩three.html) – ARJUN

回答

14
static String[][][] School= new String[1000][20][5]; 

考慮圖有3維。

所以當你插入School[0][0][0]="A1"這意味着你已經在0,0,0位置輸入了元素。

從0,0,0這將移動到位置1000,20,5。

您可以像這樣插入但是您擁有如此多的元素。

School[0][0][0]="A1" 
School[0][0][1]="A2" 
School[0][0][2]="A3" 
..... 
School[0][1][0]="B1" 
School[0][1][1]="B2" 
School[0][1][2]="B3" 
...... 

在三維數組元素看起來像

int[3][4][2] array3D 
// means Three (4x2) 2 Dimensional Arrays 

int[4][2] 
//means Four 1 dimensional arrays. 

現在如何在3D陣列添加元素?

在開始你可以直接使用

int[][][] threeDArray = 
    { { {1, 2, 3}, { 4, 5, 6}, { 7, 8, 9} }, 
     { {10, 11, 12}, {13, 14, 15}, {16, 17, 18} }, 
     { {19, 20, 21}, {22, 23, 24}, {25, 26, 27} } }; 

這是你的情況非常枯燥的事情,只要你想在每一個位置插入細節。 正如您有1000記錄。

你的陣列將有這樣

enter image description here

注意元素:它不推薦使用3D陣列用於此目的。

建議聲明一個類有三個Strings與此三個參數創建構造函數,並把getter和setter方法來獲得,並通過Objects

0

你的數組不會做你期望的事情。

想想像3D陣列一樣,每個元素都是一個點。如果你指定一個單一的索引,你實際上是在告訴計算機「好的,我想分配"A1"這個數組(在你的例子中,你試圖做類似於String[][] elementAtI = "A1";的事情)。沒有道理,是嗎?

要獲得數組中的單個元素,必須指定所有三個索引,就像在3D空間中指定所有三個座標以定位點一樣:

School[3][4][5] = "A1"; 

與3D數組相比,什麼可能是更好的想法是將對象包裝到數組中,但這不像SchoolClass[],其中每個SchoolClassnameStudents陣列,每個StudentIDname

3

我會建議,而不是使用3D陣列,你要創建一個Student類,將持有的所有一個學生的信息和一個SchoolClass的A類,將持有班級和班級名稱的學生名單,並且您可以保留Array of SchoolClass以達到目的。

這樣你就可以更好地管理它。

希望這有助於

0

首先設定值,變量字段通常用小寫開頭按慣例駱駝文本。

static String[][][] school= new String[1000][20][5]; 

其次,數組不會像這樣工作。 String [] [] []包含{{{entry ...}條目...}條目...}。 這些條目可能包含重複項,這使得它成爲不可行的方法,因爲您將在同一個數組中獲得{「3A」,「1」,「PersonName」}和{「3A」,「1」,「DifferentPersonName」}。每個陣列維度保持附加維度,又名 { 「3A」,{ 「1」,{ 「PERSONNAME」},{ 「DifferentPersonName」}}},因此

School[i] = "A1";是語法錯誤,因爲你必須把字符串[ ] []字符串中的[I] [] []:

School[i] = {{"A1","PersonName"}};

我相信在座的解決方案是使用包含HashMap。重複的條目將相互覆蓋。在這種情況下,代碼將爲:

// The actual HashMap! 
static final HashMap<String, HashMap<String, String>> school 
=new HashMap<String, HashMap<String, String>>(); 

/** 
* How to use an entry using the above HashSet: 
* 
* @param className The name of the class. 
* @param id The id. 
* @param details The details. 
*/ 
void addEntry(final String className, final String id, final String details){ 
    HashMap<String, String>value=school.get(className); 
    // If the class already exists, use it, otherwise make new HashMap. 
    (value==null ? value = new HashMap<String, String>() : value) 
    // Here is how to put in the value mapping. 
    .put(id, details); 
    // Put it back in. This updates the entry. 
    school.put(value); 
} 

/** 
* How to get (iterate through) entries from the above HashSet: 
* 
* @return An array of students in format "class id details" 
*/ 
String[] getStudentsSet(){ 
    // This is an iterator. 
    Iterator<Entry<String, HashMap<String, String>>>iterator= 
     school.entrySet().iterator(); 
    Entry<String, HashMap<String, String>>next; 
    String now; 
    // This is for testing 
    final ArrayList<String>list=new ArrayList<String>(); 
    while(iterator.hasNext()){ 
    // Load new class name 
    now=(next=iterator.next()).getKey(); 
    Iterator<Entry<String, String>>iteratorlv2=next.entrySet().iterator(); 
    while(iterator.hasNext()){ 
     final Entry<String, String>entry=iterator.next(); 
     /* This is the student from class "now", id "entry.getKey()", and details "entry.getValue()" 
     * Change this line to what you want, or what you would like to use entries for. 
     */ 
     final String student=now+" "+entry.getKey()+" "+entry.getValue(); 
     // This is for testing 
     list.add(student); 
    } 
    } 
    // This is what prints to the console so you know this thing works. 
    for(final String o:list) System.out.println(o); 
    return list.toArray(new String[list.size()]); 
}