Java 关于变量参数和匿名数组

Java 关于变量参数和匿名数组,java,Java,请告知我如何将下面的程序转换为变量参数,这是java 5引入的新功能,现在我正在使用匿名内部数组 public class AnnonymousArrayExample { public static void main(String[] args) { //calling method with anonymous array argument System.out.println("first total of numbers: " + sum(n

请告知我如何将下面的程序转换为变量参数,这是java 5引入的新功能,现在我正在使用匿名内部数组

public class AnnonymousArrayExample {

    public static void main(String[] args) {

        //calling method with anonymous array argument
        System.out.println("first total of numbers: " + sum(new int[]{ 1, 2,3,4}));
        System.out.println("second total of numbers: " + sum(new int[]{ 1, 2,3,4,5,6,}));

    }

    //method which takes an array as argument
    public static int sum(int[] numbers){
        int total = 0;
        for(int i: numbers){
            total = total + i;
        }
        return total;
    }
}

让你的方法签名像这样
公共静态整数和(整数…数字)

以下是有效的调用

sum();
sum(1,2,3);
sum(1,2,3,4,5);
sum(new int[] {1,2,3})

让你的方法签名像这样
公共静态整数和(整数…数字)

以下是有效的调用

sum();
sum(1,2,3);
sum(1,2,3,4,5);
sum(new int[] {1,2,3})
使用var参数

public static int sum(int... numbers){}

另请参见

使用变量参数

public static int sum(int... numbers){}

另请参见

使用
varargs(…)
更改方法签名
公共静态整数和(整数…数字)

更多关于
varargs
-

使用
varargs(…)
更改方法签名
公共静态整数和(整数…数字)


有关
varargs
-

的更多信息,只需更改参数声明(从[]更改为…),而不更改调用部分:

public class AnnonymousArrayExample {

    public static void main(String[] args) {

        // calling method with anonymous array argument
        System.out.println("first total of numbers: "
                + sum(new int[] { 1, 2, 3, 4 }));
        System.out.println("second total of numbers: "
                + sum(new int[] { 1, 2, 3, 4, 5, 6, }));

    }

    // method which takes an array as argument
    public static int sum(int... numbers) {
        int total = 0;
        for (int i : numbers) {
            total = total + i;
        }
        return total;
    }
}

只需更改参数声明(从[]更改为…),调用部分不做任何更改:

public class AnnonymousArrayExample {

    public static void main(String[] args) {

        // calling method with anonymous array argument
        System.out.println("first total of numbers: "
                + sum(new int[] { 1, 2, 3, 4 }));
        System.out.println("second total of numbers: "
                + sum(new int[] { 1, 2, 3, 4, 5, 6, }));

    }

    // method which takes an array as argument
    public static int sum(int... numbers) {
        int total = 0;
        for (int i : numbers) {
            total = total + i;
        }
        return total;
    }
}

总和法应声明如下:

public static int sum(int ... numbers)
电话应该是这样的:

sum(1,2,3,4,5)

总和法应声明如下:

public static int sum(int ... numbers)
电话应该是这样的:

sum(1,2,3,4,5)

没有“匿名内部数组”这类东西——你得到的只是一个简单的内部数组

要使用varargs数组,只需如下更改方法声明:

public static int sum(int... numbers) {
    // Code as before - the type of numbers will still be int[]
}
public static int sum(int... numbers) {
  int total = 0;
  for (int i : numbers) {
    total = total + i;
  }
  return total;
}
并将呼叫代码更改为:

System.out.println("first total of numbers: " + sum(1, 2, 3, 4));

有关更多详细信息,请参阅。

没有“匿名内部数组”这样的东西-您在那里得到的只是一个简单的内部数组

要使用varargs数组,只需如下更改方法声明:

public static int sum(int... numbers) {
    // Code as before - the type of numbers will still be int[]
}
public static int sum(int... numbers) {
  int total = 0;
  for (int i : numbers) {
    total = total + i;
  }
  return total;
}
并将呼叫代码更改为:

System.out.println("first total of numbers: " + sum(1, 2, 3, 4));

有关更多详细信息,请参阅。

sum函数如下所示:

public static int sum(int... numbers) {
    // Code as before - the type of numbers will still be int[]
}
public static int sum(int... numbers) {
  int total = 0;
  for (int i : numbers) {
    total = total + i;
  }
  return total;
}
它看起来几乎与您的原始代码一致——这是有充分理由的。据我所知,在内部,varargs由编译器使用数组进行翻译。这两个函数的签名是相同的-您将无法在代码中同时保留这两个函数。所以“省略号”语法只是语法上的糖分

以及函数的调用:

int total = sum(1,2,3,4);

语法上看起来更清晰,但实际上,它与原始代码相同-编译器将创建一个数组,用给定的值初始化它,并将其作为参数传递给函数。

sum函数如下所示:

public static int sum(int... numbers) {
    // Code as before - the type of numbers will still be int[]
}
public static int sum(int... numbers) {
  int total = 0;
  for (int i : numbers) {
    total = total + i;
  }
  return total;
}
它看起来几乎与您的原始代码一致——这是有充分理由的。据我所知,在内部,varargs由编译器使用数组进行翻译。这两个函数的签名是相同的-您将无法在代码中同时保留这两个函数。所以“省略号”语法只是语法上的糖分

以及函数的调用:

int total = sum(1,2,3,4);

语法上看起来更清晰,但实际上,它与您的原始代码相同-编译器将创建一个数组,用给定的值初始化它,并将其作为参数传递给函数。

调用部分需要做什么更改也请给出建议。!你可以把它叫做sum(1,2,3,4);//任何数量的争论呼叫部分需要做什么更改也请告知。!你可以把它叫做sum(1,2,3,4);//任意数量的参数