New features of 1.5 after the variable parameters

package test;


import java.util. *;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.locks.Lock;

import javax.print.attribute.IntegerSyntax;

import org.omg.CosNaming.NamingContextExtPackage.AddressHelper;

import privateclass.*;
import java.io. *;

public class Main {

	private static final String space_operator = " ";
	public static void main(String[] args) throws Exception {

		Scanner scanner = new Scanner(new BufferedInputStream(System.in));
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		
		
		/*
		 * New features after the variable parameter 1.5
		 */
		
		int sum = add(1,2,3,4,5,78,5);
		System.out.println(sum);
		
		
	}
	
	// variable parameters directly transferred one does not know how much int array has
	// But this can only be placed in the end of the array variable 
	/*
	 * Because the back at the beginning when we can not pass parameters
	 * 
	 */
	private static int add(int... arr)
	{
		int sum = 0;
		for (int i = 0; i < arr.length; i ++) {
			sum += arr[i];
		}
		return sum;
	}
	
	// We then placed in front of the first number is equivalent to a transfer
	/*
	 *
	private static int add(int a,int... arr)
	{
		int sum = 0;
		for (int i = 0; i < arr.length; i ++) {
			sum += arr[i];
		}
		return sum;
	}
	*/
}

  

Guess you like

Origin www.cnblogs.com/WINDZLY/p/11788700.html