Java spread operator

前端 未结 6 910
死守一世寂寞
死守一世寂寞 2021-01-01 10:11

I am not sure of the vocabulary I am using here, please correct me if I\'m wrong.

In Javascript, I had the following code:

let args = [1,2,3];

func         


        
6条回答
  •  南笙
    南笙 (楼主)
    2021-01-01 10:18

    In java there is concept of Variable Arguments, using which you can pass different numbers of arguments to same function.

    I am taking your code as an example :

    public class Foo {
        public int doSomething (int ...a) {
          int sum = 0;
          for (int i : a)
               sum += i;
            return sum;
        }
     }
    

    Now you can call this function as :

    doSomething (args)
    

    For more information you can visit below link : http://www.geeksforgeeks.org/variable-arguments-varargs-in-java/

提交回复
热议问题