Using a foreach(for each) for loop with a collection.
import java.util.ArrayList; public class MainClass { static double getAvg(ArrayList<Double> nums) { double sum = 0.0; for (double itr : nums) sum = sum + itr; return sum / nums.size(); } public static void main(String args[]) { ArrayList<Double> list = new ArrayList<Double>(); list.add(10.14); list.add(20.22); list.add(30.78); list.add(40.46); double avg = getAvg(list); System.out.println("List average is " + avg); } }