Java 数组赋值
我在准备java认证考试时看到了这个片段。谁能解释一下这是怎么回事Java 数组赋值,java,arrays,string,Java,Arrays,String,我在准备java认证考试时看到了这个片段。谁能解释一下这是怎么回事 public static void main(String args[]) { String str[] = new String[][] { { null }, new String[] { "a", "b", "c" }, { new String() } }[0]; System.out.println(str[0]); } o/p为null,与
public static void main(String args[]) {
String str[] = new String[][] {
{ null },
new String[] { "a", "b", "c" },
{ new String() }
}[0];
System.out.println(str[0]);
}
o/p为null
,与预期一致,但我对字符串的赋值感到困惑
它是一维数组还是二维数组
作业右侧的[0]
是什么意思
新字符串[]{“a”、“b”、“c”}
是如何工作的
提前谢谢
GPAR是一种一维数组
2> [0]
返回数组的第一个元素(在本例中是另一个数组)
3> newstring[]{“a”、“b”、“c”}
创建并初始化字符串数组,使其包含3个指定的字符串-对的回答可能会帮助您了解此字符串的各种语法规则这是非常难看的代码
这stringstr[]=newstring[][{{null},newstring[]{“a”、“b”、“c”},{newstring()}}[0]代码>通过一些初始化创建并初始化一个锯齿状的字符串2d数组,[0]用于选择要分配给新str
变量的第0个字符串[](值将为{null})
第二行打印一些在其他地方声明的值s[0]。我已将语句拆分为多行,以提高可读性
// First, the array is created, then it is stored into a variable.
// At last, store the result of code below into a String[].
String str[] =
// Create a two-dimensional array
new String[][] {
// Add the first element to the two-dimensional array,
// which is in fact a String[] with one null element in it.
/* element #0 */ new String[] { null },
// Add the second element to the two-dimensional array:
// it's a String[] containing three strings
/* element #1 */ new String[] { "a", "b", "c" },
// Then add the third element to the two-dimensional array,
// which is an empty string.
/* element #2 */ new String[] { new String() }
}
// Then get the first element of our two-dimensional array,
// which returns a String[] with one null element in it.
[0];
所以实际上,变量str
现在包含一个字符串[]
,索引0
为null
System.out.println(str[0]); // Prints null
最后,str[0]
被打印在屏幕上,该屏幕为null
System.out.println(str[0]); // Prints null
回答您的问题:
变量str
是一维数组。符号stringstr[]
非常难看和令人困惑;它应该是String[]str
。然后你可以更容易地看到,我们的变量是一维的
[0]
表示获取数组的第一个元素(数组总是以索引0开头)。二维数组的某个元素始终是一维数组;换句话说,二维数组是包含数组的数组。
这就是为什么String[]str=newstring[][]{…}[0]
是完全有效的
新建字符串[]{“a”、“b”、“c”}
创建一个包含三个字符串的字符串数组:“a”、“b”和“c”。
因此新字符串[]{“a”、“b”、“c”}[2]
将返回“c”
编辑
让我一步一步地解释
步骤1-这就是我们如何声明字符串[](字符串数组):
步骤2-我们还可以立即使用以下值初始化数组:
String[] myArray = new String[] {
"some value",
"another value",
"et cetera"
};
步骤2b-我们不需要提及元素的数量,因为编译器已经看到我们使用三个元素初始化数组
String[] myArray = new String[3] {
// ^
"some value", // That number
"another value", // is unnecessary.
"et cetera"
};
步骤3-由于我们声明了数组并立即对其进行初始化,因此可以省略new
语句:
String[] myArray = {
"some value",
"another value",
"et cetera"
};
步骤4接下来,我们有一个二维数组,它只不过是一个数组数组。
我们可以先初始化一维数组,然后将它们一起转储到二维数组中,如下所示:
String[] firstThree = { "a", "b", "c" };
String[] lastThree = { "x", "y", "z" };
String[][] myArray = new String[] {
firstThree,
lastThree
};
步骤5-但我们也可以立即执行:
String[][] myArray = new String[] {
new String[] { "a", "b", "c" },
new String[] { "x", "y", "z" }
};
步骤6-现在我们说我们可以省略新的
语句(参见步骤3),因为数组在初始化后立即初始化:
String[][] myArray = {
{ "a", "b", "c" },
{ "x", "y", "z" }
};
步骤7-对吗
步骤8-现在我们有了您的代码:
String str[] = new String[][] {
{ null },
new String[] { "a", "b", "c" },
{ new String() }
}[0];
System.out.println(str[0]);
让我们重写你的代码;实际上,它与您的代码片段相同
// Let us define a new two-dimensional string array, with space for three elements:
String[][] our2dArray = new String[3][];
// Then, let us fill the array with values.
// We will add a String array with exactly one element (that element is `null` by chance)
our2dArray[0] = new String[] { null };
// We define the contents for index 1 of our2dArray
our2dArray[1] = new String[] { "a", "b", "c" };
// At last, the last element:
our2dArray[2] = new String[] { new String() };
// Which is effectively the same as
// new String[] { "" };
到目前为止,我们已经初始化了数组
步骤9-那么,你看到这个片段了吗
}[0];
步骤10-这意味着我们只需获取新创建数组的第一个元素,并将该元素存储到名为str
的著名变量中
String[] str = our2dArray[0]; // Variable str now contains a
// String array (String[]) with exactly one null-element in it.
// With other words:
// str = String[] {
// 0 => null
// }
步骤11-如果我们打印str
数组的索引0,我们会得到null
System.out.println(str[0]); // Prints null
第12步-你知道吗?我认为这个示例作业太令人困惑了。但要回答第一个问题,并非2d阵列的所有部分都使用。首先初始化它,然后只使用第一个元素(索引0)。然而,你的第二个问题,我不明白。我的意思是它看起来像str[]=null;而不是str[0]=null;你说“it”看起来像str[]=null
。但“它”指的是什么?换言之:什么看起来像str[]=null
?让我用这种方式解释我的疑问,字符串s[]=null;系统输出打印项次//打印空的System.out.println(s[0])//空指针异常。我的第二个问题是,我上面代码片段中的代码似乎将空值分配给数组本身,而不是数组的第一个元素,即与上面代码类似的元素。在我以前的代码中,str[0]以哪种方式获取空值@这就是为什么我会打这个例子的创造者一巴掌。正在解释数组,然后涉及null
值。但我想我需要比这篇评论更多的空间来向你们解释。