Java 3D数组赋值

我有一个看起来像这样的数组

static String[][][] School= new String[1000][20][5]; 
  • 在第一个括号中,我保存了类名
  • 在第二个我保存学生的身份证
  • 在第三部分,我保存了关于学生的信息(他的姓名,姓氏等)。

首先,我分配所有的class级名称,之后我将每个class级分配给学生ID,然后我可以填写他们的信息。

我该怎么做? 我试过这个例子

 School[i] = "A1"; 

但它不起作用。

编辑:或者还有另一种方法来保存这3件事吗? (class级名称,学生及其信息)

 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" ...... 

在3D数组元素看起来像

 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条记录。

你的数组将有这样的元素

在此处输入图像描述

注意建议不要将3Darrays用于此目的。

建议使用这三个参数声明一个带有三个Strings的类创建构造函数,并将getter和setter设置为通过Objects获取和设置值

我建议您不要使用3D数组,而是创建一个Student类,它将保存学生和A Class for SchoolClass所有信息, SchoolClass将保存class级和class级名称中的学生列表,您可以维护一个Array of SchoolClass服务Array of SchoolClass目的。

这样您就可以更好地管理它。

希望这可以帮助

您的arrays将无法按预期执行操作。

将数组视为3D数组,每个元素都是一个点。 如果你指定一个索引,你实际上是告诉计算机“好的,我想把"A1"分配给数组的这个片段 (在你的例子中,你试图做一些类似于String[][] elementAtI = "A1"; )。现在没有意义,是吗?

要获取数组中的单个元素,您必须指定所有三个索引,就像在3D空间中指定所有三个坐标来定位点一样:

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

什么比3Darrays更好的想法是对象。 将所有内容打包到一个数组中是有效的,但这不像拥有SchoolClass[]那样可读,其中每个SchoolClass都有一个name和一组Students ,每个Student都有一个IDname等。

首先,变量字段通常以小写forms开头,按照惯例使用驼峰文本。

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

其次,数组不能像这样工作。 String [] [] []持有{{{entry …} entry …}条目…}。 这些条目可能包含重复项,这使得它成为一种不可行的方法,因为您将在同一个数组中获得{“3A”,“1”,“PersonName”}和{“3A”,“1”,“DifferentPersonName”}。 每个数组维度都包含其他维度,即{“3A”,{“1”,{“PersonName”},{“DifferentPersonName”}}},所以

School[i] = "A1"; 是语法错误,因为你必须将String [] []放在String [i] [] []中:

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

我相信这里的解决方案是使用HashMaps。 重复的条目将相互覆盖。 在这种情况下,代码将是:

 // The actual HashMap! static final HashMap> school =new HashMap>(); /** * 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){ HashMapvalue=school.get(className); // If the class already exists, use it, otherwise make new HashMap. (value==null ? value = new HashMap() : 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>>iterator= school.entrySet().iterator(); Entry>next; String now; // This is for testing final ArrayListlist=new ArrayList(); while(iterator.hasNext()){ // Load new class name now=(next=iterator.next()).getKey(); Iterator>iteratorlv2=next.entrySet().iterator(); while(iterator.hasNext()){ final Entryentry=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()]); }