Java 3D 数组赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23630668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java 3D array assign values
提问by user3549340
I have an array that looks like this
我有一个看起来像这样的数组
static String[][][] School= new String[1000][20][5];
- In the first bracket I save the class name
- In the second I save an ID of a student
- In the third I save information about the student (his name, family name etc).
- 在第一个括号中,我保存了类名
- 在第二个我保存了一个学生的 ID
- 在第三个中,我保存了有关学生的信息(他的姓名、姓氏等)。
First I assign all the class names, after that I assign to every class its student ID and then I can fill in their information.
首先我分配所有班级名称,然后我为每个班级分配学生ID,然后我可以填写他们的信息。
How can I do it? I tried it with for example
我该怎么做?例如,我尝试过
School[i] = "A1";
but it's not working.
但它不起作用。
EDIT: Or is there an other way to save this all 3 things? (class name, its students and its iformation)
编辑:或者有其他方法可以保存这三样东西吗?(班级名称、学生和信息)
采纳答案by CoderCroc
static String[][][] School= new String[1000][20][5];
Consider figure which has 3 Dimension.
考虑具有 3 维的图形。
So when you insert School[0][0][0]="A1"
it means you have entered element at 0,0,0 position.
所以当你插入School[0][0][0]="A1"
它意味着你已经在 0,0,0 位置输入了元素。
From 0,0,0 this will move upto the position 1000,20,5.
从 0,0,0 这将向上移动到位置 1000,20,5。
You can insert like this But you have so many elements.
你可以像这样插入 但是你有这么多元素。
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"
......
In 3D array elements look like
在 3D 数组元素看起来像
int[3][4][2] array3D
// means Three (4x2) 2 Dimensional Arrays
int[4][2]
//means Four 1 dimensional arrays.
Now how to add elements in 3D array?
现在如何在 3D 数组中添加元素?
At Start you can directly use
在开始时,您可以直接使用
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} } };
This is very tedious task in your case as you want to insert details at every position.
As you have 1000
records.
在您的情况下,这是一项非常繁琐的任务,因为您想在每个位置插入详细信息。因为你有1000
记录。
Your array will have elements like this
您的数组将具有这样的元素
NOTE:It's not recommended to use 3D array for this purpose.
注意:不建议为此目的使用 3D 阵列。
Suggestion:Declare a class with three Strings
create constructor with this three parameters and put getter and setters to get and set values via Objects
建议:使用这三个参数声明一个具有三个Strings
创建构造函数的类,并通过 getter 和 setter 来获取和设置值Objects
回答by awksp
Your array won't do what you expect it to do.
你的阵列不会做你期望它做的事情。
Think of the array like a 3D array, with each element a point. If you specify a single index, you're essentially telling the computer "OK, I want to assign "A1"
to this slice of the array(in your example, you're trying to do something akin to String[][] elementAtI = "A1";
). Now that doesn't make sense, does it?
把数组想象成一个 3D 数组,每个元素都是一个点。如果您指定单个索引,您实际上是在告诉计算机“好吧,我想分配"A1"
给数组的这个切片(在您的示例中,您正在尝试执行类似于 的操作String[][] elementAtI = "A1";
)。现在这没有意义, 可以?
To get to a single element in the array, you have to specify all three indices, much like how in 3D space you have to specify all three coordinates to locate a point:
要获取数组中的单个元素,您必须指定所有三个索引,就像在 3D 空间中您必须指定所有三个坐标来定位一个点一样:
School[3][4][5] = "A1";
What might be a better idea than a 3D array is objects. Packing everything into an array works, but that's not as readable as having a SchoolClass[]
, where each SchoolClass
has a name
and an array of Students
, and each Student
has an ID
, name
, etc.
可能比 3D 数组更好的想法是对象。包装一切到一个数组的作品,但是这不是为可读为具有SchoolClass[]
,其中每个SchoolClass
具有name
的和阵列Students
,并且每个Student
具有一个ID
,name
等等。
回答by Sanjeev
I will suggest instead of using a 3D array, you shall create a Student
Class that will hold all the information for a student and A Class for SchoolClass
that will hold a list of students in the class and name of class and you can maintain an Array of SchoolClass
to serve the purpose.
我建议您不要使用 3D 数组,而是创建一个Student
类来保存学生的所有信息,创建一个类来保存班级SchoolClass
中的学生列表和班级名称,您可以维护一个Array of SchoolClass
来为学生提供服务目的。
This way you will be able to manage it better.
通过这种方式,您将能够更好地管理它。
Hope this helps
希望这可以帮助
回答by Unihedron
First of all, variable fields usually start with lowercase with camel text by convention.
首先,按照惯例,变量字段通常以小写字母开头,并带有驼峰文本。
static String[][][] school= new String[1000][20][5];
Secondly, arrays do not work like this. String[][][] holds {{{entry...}entry...}entry...}.
The entries may contain duplicates, which makes it an unfeasible method as you will get {"3A", "1", "PersonName"}
and {"3A", "1", "DifferentPersonName"}
within the same array. Each array dimension is holding additional dimensions, aka
{"3A", {"1", {"PersonName"}, {"DifferentPersonName"}}}
, so
其次,数组不是这样工作的。String[][][] 保存 {{{entry...}entry...}entry...}。该条目可能包含重复的,这使得它的不可行的方法,你会得到{"3A", "1", "PersonName"}
和{"3A", "1", "DifferentPersonName"}
在同一阵列中。每个数组维度都持有额外的维度,也就是
{"3A", {"1", {"PersonName"}, {"DifferentPersonName"}}}
,所以
School[i] = "A1";
is syntax error, because you must put String[][] in String[i][][]:
School[i] = "A1";
是语法错误,因为你必须把 String[][] 放在 String[i][][] 中:
School[i] = {{"A1","PersonName"}};
School[i] = {{"A1","PersonName"}};
I believe a solution here would be to use HashMaps. Repeated entries will overwrite each other. In this case, the code will be:
我相信这里的解决方案是使用 HashMaps。重复输入将相互覆盖。在这种情况下,代码将是:
// 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()]);
}