Java 我对这个程序中的z类型数组声明有一个问题

Java 我对这个程序中的z类型数组声明有一个问题,java,arrays,Java,Arrays,有谁能解释一下这个程序的输出是如何为14的,以及为什么我们在函数go参数列表中将数组声明为int[]…z public class Venus { public static void main(String[] args) { int [] x = {1,2,3}; int y[] = {4,5,6}; new Venus().go(x,y); } void go(int[]... z) { f

有谁能解释一下这个程序的输出是如何为14的,以及为什么我们在函数go参数列表中将数组声明为int[]…z

public class Venus 
{
    public static void main(String[] args) 
    {
      int [] x = {1,2,3};
      int y[] = {4,5,6};
      new Venus().go(x,y);
    }

    void go(int[]... z) 
    {
        for(int[] a : z)
          System.out.print(a[0]);
    }
}

首先,go方法获取一个int数组数组:

void go(int[]... z) 
其次,您传递了x和y数组

new Venus().go(x,y);
然后要求打印每个数组中的第一个元素

for(int[] a : z) // a is a temp veriable hold one element from the passed array of array and z represents the array of int arrays which is {x, y}
      System.out.print(a[0]);

所以输出是14

int[]…
符号就是符号

这意味着您的
go(int[]…z)
方法接受多个
int[]
作为参数。在方法内部,
z
int类型的
int[][]


因此,
go
所做的是,对于
z
中的每个int数组
a
,它打印
a
的第一个元素。这就是为什么要打印
1
x[0]
)和
4
y[0]
)的原因=>
14
方法
go
接受一个长度可变的int数组序列,遍历参数,并打印每个数组的第一个元素。当您传递x和y时,它将打印x的第一个元素(即1)和y的第一个元素(即4),因此为14。

  int [] x = {1,2,3};
  int y[] = {4,5,6};
  new Venus().go(x,y);
当它调用go(int[]…z)时,实际上是这样的

 int[][] z = {{1,2,3},{4,5,6}};
因为它的数组大小是2。所以这个循环会转两圈

循环1:

int[]a = z[0]; // z[0] is {1,2,3}  
System.out.print(a[0]); // a[0] is 1
int[]a = z[1];  //z[1] is {4,5,6}
System.out.print(a[0]); // a[0] is 4
循环2:

int[]a = z[0]; // z[0] is {1,2,3}  
System.out.print(a[0]); // a[0] is 1
int[]a = z[1];  //z[1] is {4,5,6}
System.out.print(a[0]); // a[0] is 4

难道你不应该知道你的代码在做什么吗?建议你试着用调试器检查一下,这会帮助你理解。在我看来,这是一种练习。没有人会写这样的代码。。。这是Java中数组语法的一个很好的概述,您可能应该试着自己去理解它。我想这就是困惑所在。