java optional parameters [duplicate]
问题 This question already has answers here : Java optional parameters (17 answers) Closed 6 months ago . I want to write an average method in java such that it can consume N amount of items, returning the average of them: My idea was: public static int average(int[] args){ int total = 0; for(int i=0;i<args.length;i++){ total = total + args[i]; } return Math.round (total/args.length); } //test it average(1,2,3) // s**hould return 2. how can I change my method to consume any amount of parameters