/**
* @author hornick
* This class/program creates arrays and ArrayLists, fills them with values, and computes the averages of the values
*/
public class CollectionApp {
/**
* @param args
*/
public static void main(String[] args) {
double numArray[] = null; // a primitive array that holds a collection of doubles
ArrayList<Double> numList = null; // a JCF collection class that holds a collection of Doubles
System.out.println("Enter a low and a high value: ");
Scanner kbd = new Scanner(System.in);
int low = kbd.nextInt();
int high = kbd.nextInt();
numArray = fillArray( low, high ); // create and fill the array
numList = fillArrayList( low, high ); // create and fill the ArrayList
double avg1 = getArrayAvg( numArray );
double avg2 = getArrayListAvg( numList );
System.out.println("Array avg=" + avg1 + ", ArrayList avg=" + avg2 );
}
/**
* This method allocates and fills an array with the values between the specified low and high values
* @param low - the lowest value in the array
* @param high - the highest value in the array
* @return - updated reference to the created non-empty array of doubles
*/
public static double[] fillArray( int low, int high ) {
double[] nums = new double[high-low+1]; // create the actual array just big enough to hold all the values
int index = 0;
for(int i=low; i<=high; i++) {
nums[index++] = i; // stuff the values into the array at specific locations
}
return nums;
}
/**
* This method allocates and fills an ArrayList with the values between the specified low and high values
* @param low - the lowest value in the ArrayList
* @param high - the highest value in the ArrayList
* @return - updated reference to the created non-empty ArrayList of Doubles
*/
public static ArrayList<Double> fillArrayList( int low, int high ) {
ArrayList<Double> nums = new ArrayList<Double>(); // create the actual ArrayList (size is automatically managed internally)
for(int i=low; i<=high; i++) {
nums.add((double)(i)); // add values to the ArrayList; size grows automatically as needed
}
return nums;
}
/**
* This method computes the average of the values in the specified array
* @param numArray - reference to an array of values, created elsewhere
* @return - the average of the values within the array
*/
public static double getArrayAvg( double numArray[] ) {
double sum = 0.0;
for(int i=0; i<numArray.length; i++) {
sum += numArray[i]; // add to the sum by retrieving values from specific array locations
}
return sum/numArray.length;
}
/**
* This method computes the average of the values in the specified ArrayList
* @param numList - reference to an ArrayList of values, created elsewhere
* @return - the average of the values within the ArrayList
*/
public static double getArrayListAvg( ArrayList<Double> numList ) {
double sum = 0.0;
for(int i=0; i<numList.size(); i++) {
sum += numList.get(i); // add to the sum by retrieving values from specific ArrayList locations
}
// OR, you can use this method below to iterate through a Collection (such as an ArrayList) - more on this in CS2852
// for( double x: numList ) {
// sum += x;
// }
return sum/numList.size();
}
}