Is there possibility of sum of ArrayList without looping

后端 未结 13 1326
无人共我
无人共我 2020-11-27 04:24

Is there possibility of sum of ArrayList without looping?

PHP provides sum(array) which will give the sum of array.

The PHP code is

13条回答
  •  有刺的猬
    2020-11-27 04:55

    Once java-8 is out (March 2014) you'll be able to use streams:

    If you have a List

    int sum = list.stream().mapToInt(Integer::intValue).sum();
    

    If it's an int[]

    int sum = IntStream.of(a).sum();
    

提交回复
热议问题