2014-05-13 122 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()]); 
}