gpt4 book ai didi

java - 数组的赋值

转载 作者:行者123 更新时间:2023-11-30 03:41:55 25 4
gpt4 key购买 nike

我在准备 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,但我对 String 数组的赋值感到困惑。

  1. 是一维数组还是二维数组?
  2. 赋值语句右侧的 [0] 是什么意思?
  3. new String[] { "a", "b", "c"} 是如何工作的?

提前致谢GPAR

最佳答案

我将语句分成多行,提高了可读性

// 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 现在包含一个 String[] ,索引 0null

最后,屏幕上打印了 str[0] ,原来是 null

回答您的问题:

  1. 变量 str 是一个一维数组。符号 String str[] 非常难看且令人困惑;它应该是 String[] str 。然后你可以更容易地看到我们的变量是一维的。
  2. [0] 表示获取数组的第一个元素(数组始终从索引 0 开始)。二维数组的某个元素始终是一维数组;换句话说,二维数组是包含数组的数组。
    这就是为什么 String[] str = new String[][] { ... }[0] 完全有效。
  3. new String[] { "a", "b", "c" } 创建一个包含三个字符串的字符串数组:“a”、“b”和“c”。
    因此 new String[] { "a", "b", "c" }[2] 将返回“c”。

编辑

让我一步步解释一下。

第 1 步 — 这是我们声明 String[](字符串数组)的方式:

String[] myArray = new String[numberOfElements];

第 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 — 现在我们说我们可以省略 new 语句(参见步骤 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 步 — 你明白了吗?

关于java - 数组的赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26649327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com