Java 创建二维字符串数组 anArray[2][2]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20359228/
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
Create a two dimensional string array anArray[2][2]
提问by ParamoreBaby
I'm currently on a self learning Java course and am completely stumped at one of my assignments, can anyone give me some pointers please? Bear in mind that I am completely new to Java so I need it to be as simple as.
我目前正在自学 Java 课程,并且完全被我的一项作业难住了,谁能给我一些指点?请记住,我对 Java 完全陌生,所以我需要它尽可能简单。
The question: 3. Create a Java program called TwoDimArray and implement the following:
问题: 3. 创建一个名为 TwoDimArray 的 Java 程序并实现以下内容:
Create a two dimensional string array anArray[2][2].
Assign values to the 2d array containing any Country and associated colour.
Example:
France Blue
Ireland Green
Output the values of the 2d array using nested for loops.
The code I have come up with that is also not working:
我想出的代码也不起作用:
public class TwoDimArray {
public static void main(String[] args) {
char Ireland = 0;
char Green = 1;
char England = 2;
char White = 3;
char firstArray[][] = {{Ireland},{Green}};
char secondArray[][] = {{England},{White}};
System.out.println();
display(firstArray);
System.out.println();
display(secondArray);
}
public static void display(char a[][]) {
for (char row = 0; row < a .length; row++) {
for (char col = 0; col < a [row] .length; col++) {
System.out.print(a[row][col] + " ");
}
System.out.println();
}
}
}
Can anyone give me some pointers? I can't seem to find anything that helps me on the web so thought I'd come here and ask. Thanks in advance.
谁能给我一些指点?我似乎在网上找不到任何对我有帮助的东西,所以我想我会来这里问。提前致谢。
采纳答案by theGreenCabbage
Looks like this is what you want
看起来这就是你想要的
int columns = 2;
int rows = 2;
String[][] newArray = new String[columns][rows];
newArray[0][0] = "France";
newArray[0][1] = "Blue";
newArray[1][0] = "Ireland";
newArray[1][1] = "Green";
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.println(newArray[i][j]);
}
}
Here I'll explain the code:
下面我来解释一下代码:
This declares the size of your new 2D array. In Java (and most programming languages), your first value starts at 0, so the size of this array is actually 2 rows by 2 columns
这声明了新二维数组的大小。在 Java(和大多数编程语言)中,您的第一个值从 0 开始,因此该数组的大小实际上是 2 行 x 2 列
int columns = 2;
int rows = 2;
Here you are using the type String[][]
to create a new 2D array with the size defined by [rows][columns]
.
在这里,您将使用该类型String[][]
创建一个新的二维数组,其大小由[rows][columns]
.
String[][] newArray = new String[columns][rows];
You assign the values by its placement within the array.
您可以通过其在数组中的位置来分配值。
newArray[0][0] = "France";
newArray[0][1] = "Blue";
newArray[1][0] = "Ireland";
newArray[1][1] = "Green";
Looping through i
would loop through the rows, and looping through j
would loop through the columns. This code loops through all rows and columns and prints out the values in each index.
循环遍历i
行,循环遍历j
列。此代码循环遍历所有行和列并打印出每个索引中的值。
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.println(newArray[i][j]);
}
}
Alternatively, assignment can be a one-liner:
或者,分配可以是单行的:
String[][] newArray = {{"France", "Blue"}, {"Ireland", "Green"}};
But I don't like this way, as when you start dealing with larger sets of data (like 10,000+ points of data with many columns), hardcoding it in like this can be rough.
但我不喜欢这种方式,因为当您开始处理更大的数据集时(例如 10,000 多个具有多列的数据点),像这样硬编码可能会很粗糙。