1 class Demo 2 { 3 public static int sum(int ... data) { //此处可以传递一个数组,也可以是多个参数 4 int sum = 0; 5 for (int i : data) 6 sum = sum + i; 7 return sum; 8 } 9 } 10 public class Variable { 11 public static void main(String[] args) { 12 System.out.println(Demo.sum(1,2,3,4,5)); 13 System.out.println(Demo.sum(new int [] {1,2,4,5,3,4})); 14 } 15 }
从JDK1.5开始,为了方便开发者进行可变参数的定义,就提供了此功能,喵~~
原文地址:https://www.cnblogs.com/fairy-land/p/11963728.html
时间: 2024-10-12 10:45:03