方法参数中的“”三点式“”

JDK 5.0 provides a feature to pass unspecified number of argument to a methodJava treats the variable-length argument list as an array. This is represented by argument type followed by three dots in the declaration of the Method.

The method int sum(int…numbers) can take any number of parameters of type integer.

public int sum(int...numbers) {

        int sum=0;
        for (int d : numbers )         
            sum =sum + d;
        return sum;
    }

    public static void main(String[] args) {
        int i=3;
        int j=6;
        int k=9;
        System.out.println("Sum  of two numbers"+sum(i,j));
        System.out.println("Sum of three number"+sum(i,j,k));
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s