java中的2D数组并将其用作1D

时间:2022-12-29 18:49:38

I was going through some basic MCQ questions in Java and I was unable to understand this one.

我正在阅读Java中的一些基本MCQ问题,但我无法理解这一问题。

 public class CommandArgsThree {
     public static void main(String[] args) {
         String[][] argCopy = new String[2][2];
         int x;
         argCopy[0] = args;
         x = argCopy[0].length;
         for (int y = 0; y < x; y++) {
             System.out.print(" " + argCopy[0][y]);
         }
     }
 }

and the command-line invocation is

并且命令行调用是

java CommandArgsThree 1 2 3

java CommandArgsThree 1 2 3

Now what I can't understand is that the argCopy has been declared as a 2D array then how can it be used as 1D couple of lines later where argCopy[0] has been assigned the value of args?

现在我无法理解的是,argCopy已被声明为2D数组,那么如何在argCopy [0]被赋值为args的情况下将其用作1D几行?

P.S: I also know that argCopy[0] is 1D array that's why I am asking how can we use the 2D array as 1D here?Means is it legal to do so?

P.S:我也知道argCopy [0]是一维数组,这就是为什么我问我们如何在这里使用二维数组作为一维?意味着这样做是否合法?

6 个解决方案

#1


2  

argCopy is a 2D array aka an array of arrays. Therefore, the elements argCopy[0] and argCopy[1] will hold 1D arrays of default size 2. And since args is a 1D array, argCopy[0] can be reassigned from an empty array of size 2 to the array known as args. To access the individual elements of each 1D array within the 2D array, you not only have to identify the index of the array but also the index of the element. For example, argCopy[0][0] will let you access the first element of the first array. If the concept of argCopy[0].length confuses you, all it means is the number of elements of the first array. In your case, it started out as 2, but once you reassigned argCopy[0] to args, it changed to the length of args.

argCopy是一个2D数组,也就是一个数组数组。因此,元素argCopy [0]和argCopy [1]将保存默认大小为2的1D数组。由于args是1D数组,因此可以将argCopy [0]从大小为2的空数组重新分配给称为args的数组。要访问2D数组中每个1D数组的各个元素,您不仅需要识别数组的索引,还要识别元素的索引。例如,argCopy [0] [0]将允许您访问第一个数组的第一个元素。如果argCopy [0] .length的概念让你感到困惑,那么它就意味着第一个数组的元素数量。在你的情况下,它从2开始,但是一旦你将argCopy [0]重新分配给args,它就变成了args的长度。

#2


3  

A 2D array is an array of arrays. So argCopy[0] is the array at index 0 which is a 1D array.

2D数组是一个数组数组。所以argCopy [0]是索引0处的数组,它是一维数组。

#3


1  

Well, argCopy is 2D but argCopy[0] which is assigned to is 1D.

好吧,argCopy是2D但argCopy [0]被赋予的是1D。

#4


1  

args is assigned as the first element of argCopy at position 0. ;)

args被指定为位置0的argCopy的第一个元素。;)

#5


0  

 public class CommandArgsThree 
{
public static void main(String [] args) 
{
    String [][] argCopy = new String[2][2]; //Declaration and initialization of argCopy which is a 2D array.
    int x; //Declaration of an integer x
    argCopy[0] = args; // In the first index in the 2D array put the 1D String array args
    x = argCopy[0].length; //Put the length of the array in the 1st index of the 2D array argCopy into x
    for (int y = 0; y < x; y++) // For loop that runs from 0 till it reaches the value of x
    {
        System.out.print(" " + argCopy[0][y]); // Show in the console what is in the array at index y in the 1st index of the 2D array argCopy
    }
}
}

Commented

评论

#6


0  

You can do this because a 2d array is an array of arrays. Thus when you do something like argCopy[0] you essentially asking the first array how many arrays does you hold?

您可以这样做,因为2d数组是一个数组数组。因此,当您执行类似argCopy [0]的操作时,您基本上会询问第一个数组您拥有多少个数组?

See this Oracle tutorial, part Creating, Initializing, and Accessing an Array

请参阅此Oracle教程,部分创建,初始化和访问阵列

#1


2  

argCopy is a 2D array aka an array of arrays. Therefore, the elements argCopy[0] and argCopy[1] will hold 1D arrays of default size 2. And since args is a 1D array, argCopy[0] can be reassigned from an empty array of size 2 to the array known as args. To access the individual elements of each 1D array within the 2D array, you not only have to identify the index of the array but also the index of the element. For example, argCopy[0][0] will let you access the first element of the first array. If the concept of argCopy[0].length confuses you, all it means is the number of elements of the first array. In your case, it started out as 2, but once you reassigned argCopy[0] to args, it changed to the length of args.

argCopy是一个2D数组,也就是一个数组数组。因此,元素argCopy [0]和argCopy [1]将保存默认大小为2的1D数组。由于args是1D数组,因此可以将argCopy [0]从大小为2的空数组重新分配给称为args的数组。要访问2D数组中每个1D数组的各个元素,您不仅需要识别数组的索引,还要识别元素的索引。例如,argCopy [0] [0]将允许您访问第一个数组的第一个元素。如果argCopy [0] .length的概念让你感到困惑,那么它就意味着第一个数组的元素数量。在你的情况下,它从2开始,但是一旦你将argCopy [0]重新分配给args,它就变成了args的长度。

#2


3  

A 2D array is an array of arrays. So argCopy[0] is the array at index 0 which is a 1D array.

2D数组是一个数组数组。所以argCopy [0]是索引0处的数组,它是一维数组。

#3


1  

Well, argCopy is 2D but argCopy[0] which is assigned to is 1D.

好吧,argCopy是2D但argCopy [0]被赋予的是1D。

#4


1  

args is assigned as the first element of argCopy at position 0. ;)

args被指定为位置0的argCopy的第一个元素。;)

#5


0  

 public class CommandArgsThree 
{
public static void main(String [] args) 
{
    String [][] argCopy = new String[2][2]; //Declaration and initialization of argCopy which is a 2D array.
    int x; //Declaration of an integer x
    argCopy[0] = args; // In the first index in the 2D array put the 1D String array args
    x = argCopy[0].length; //Put the length of the array in the 1st index of the 2D array argCopy into x
    for (int y = 0; y < x; y++) // For loop that runs from 0 till it reaches the value of x
    {
        System.out.print(" " + argCopy[0][y]); // Show in the console what is in the array at index y in the 1st index of the 2D array argCopy
    }
}
}

Commented

评论

#6


0  

You can do this because a 2d array is an array of arrays. Thus when you do something like argCopy[0] you essentially asking the first array how many arrays does you hold?

您可以这样做,因为2d数组是一个数组数组。因此,当您执行类似argCopy [0]的操作时,您基本上会询问第一个数组您拥有多少个数组?

See this Oracle tutorial, part Creating, Initializing, and Accessing an Array

请参阅此Oracle教程,部分创建,初始化和访问阵列