0

Please advise How can I convert the below program into variable arguments that is the new feature introduced by java 5, rite now I am making use of anonymous inner arrays ..

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;
    }
}

7 Answers7

2

Make your method signature like this public static int sum(int ... numbers)

The following are valid invocations

sum();
sum(1,2,3);
sum(1,2,3,4,5);
sum(new int[] {1,2,3})
Shiva Kumar
  • 3,111
  • 1
  • 26
  • 34
2

Use var-args

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

Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
1

There's no such thing as an "anonymous inner array" - what you've got there is simply an array creation expression.

To use varargs arrays, you just change the method declaration like this:

public static int sum(int... numbers) {
    // Code as before - the type of numbers will still be int[]
}

And change the calling code to:

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

See section 8.4.1 of the Java Language Specification for more details, or the Java 1.5 language guide.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

to use varargs (...) change you method signature public static int sum(int... numbers).

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(new int[]{ 1, 2,3,4}));
System.out.println("second total of numbers: " + sum(new int[]{ 1, 2,3,4,5,6,}));

More on varargs - Reference Document

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

Just change your parameter declaration (from [] to ...) and no change in calling part :

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;
    }
}
Iswanto San
  • 18,263
  • 13
  • 58
  • 79
0

The sum method should be declared as follows:

public static int sum(int ... numbers)

And the call should look like:

sum(1,2,3,4,5)
Eyal Schneider
  • 22,166
  • 5
  • 47
  • 78
0

The sum function would look like this:

public static int sum(int... numbers) {
  int total = 0;
  for (int i : numbers) {
    total = total + i;
  }
  return total;
}

It looks almost indentical to your original code - and for good reason. As far as I know, internally, the varargs are translated by the compiler using arrays. The signatures of both functions are identical - you would not be able to keep both in the code. So the "ellipsis" syntax is just syntactic sugar.

And the invocation of the function:

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

Looks cleaner syntactically, but under the hood, it is the same as your original code - the compiler will create an array, initialize it with the given values and pass it to the function as parameter.

Santik
  • 85
  • 3
  • 11