1

By shortest form I mean least number of lines of java codes. I would like to get average value of items of an ArrayList of integer values. For example for the ArrayList ar:

ArrayList<Integer> ar = new ArrayList<Integer>(Arrays.asList(1,2,3,5,8,13,21));

I am looking for something like

ar.Average()

or

ar.Sum()

or

Arrays.Average(ar)

PS. I know we can do it in a loop. Just looking for a shorter alternative;

C graphics
  • 7,308
  • 19
  • 83
  • 134
  • 1
    Related (but not exact duplicate): ["Iterable Sum in Java?"](http://stackoverflow.com/q/4758341/978917) – ruakh Feb 21 '13 at 23:01
  • 1
    So you're looking for a method that is already present? Checked the [docs](http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html)? – Lukas Knuth Feb 21 '13 at 23:01
  • 3
    It's about 5 lines in a loop, and you can put that into a method which you call from elsewhere. Are you *really* that bothered by introducing one new - and short - method? – Jon Skeet Feb 21 '13 at 23:02
  • I know, but see it is a classic problem and I thought there should be something in ArrayList or similar utilities by now. – C graphics Feb 21 '13 at 23:04
  • I checked the doc for ArrayLisr and Arrays, there is none. That is why I said SIMILAR – C graphics Feb 21 '13 at 23:06
  • 1
    ArrayList can contain anything, not just integers, so it doesn't make sense that it would contain math specific functions. – Ren Feb 21 '13 at 23:07
  • Do you mean the shortest do-it-yourself implementation, or do you mean the shortest way to do it using the standard library, or do you mean the shortest way to do it using free-to-use libraries? As a side note, Dijkstra in "the humble programmer" thought this phenomenon of "one-liners" is counterproductive since it diverts attention from what is essential about quality programming. – G. Bach Feb 21 '13 at 23:12
  • I know that's example code but method first letter should be lowercase – Jimmt Feb 21 '13 at 23:59

4 Answers4

0

You could use a Math library such as Apache Commons Math.

http://commons.apache.org/math/apidocs/index.html

Specifically a SummaryStaticics object (http://commons.apache.org/math/apidocs/src-html/org/apache/commons/math3/stat/descriptive/SummaryStatistics.html#line.123). Though for something like average, it probably isn't worth importing a library.

Ren
  • 3,395
  • 2
  • 27
  • 46
0

The commons math library contains a mean as well as whatever else you probably want for simple statistics. Perhaps exactly what your looking for?

Steinar
  • 5,860
  • 1
  • 25
  • 23
0

If you're already using Java 8:

double average = Arrays.asList(1, 2, 3, 4)
                       .stream()
                       .map(Integer::intValue)
                       .average()
                       .getAsDouble();
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Lines of code is a always a quite meaningless measure, but if that is what you care about:

int sum=0;for(Integer i:ar)sum += i;System.out.println(sum);
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75