/**************************************************************** * Class List.java * Author: Yanni Giftakis * Date: July 9, 2004 * purpose: to maintain a list of double values * * private: * values[] - an array of double values * * public: * List() - default constructor initialise values array to zero size * * add() - adds a new value to end of the array * get() - returns the array element value at index i * length() - returns the length (# of elements) in the array * shrink() - reduces the array by 1 element; looses first element * swap() - swaps the values at array index i, j * toString() - returns a String of a formatted display of the array * total() - returns the total (summation) of elements in the array * * Note: the array values[] will never reference null, at the least it * will reference an array with no elements; it initialised to this * size in the constructor */ public class List { private double values[]; // referece to an array of double values //-------------------------------------------------------- /******************************************************* List() - def. constructor, reference values[] to an initial array of zero size; this avoids any "references to null" exception errors later. */ public List() { values = new double[0]; // point to an array of zero size }// end of List() /********************************************************** * add() - increases the array size by one, add places * the newvalue at the end. */ public void add(double newvalue) { // new array (extension of values), one size longer than current double[] newArray = new double [length()+1]; //----------------------------------------------------- // loop for all old array values; if prev. empty, no looping occurs for (int i=0; i 1) { newArray = new double [length()-1]; // new, smaller list by 1 // loop through elements in new array; skip over first in current ray for (int i=0; i<(length()-1); i++) newArray[i] = values[i+1]; // copy i+1 element from current array } values = newArray; // set values array to the new list newArray = null; // referene new Array to null; optional }// end of shrink() /********************************************************** * swap() - exchanges the values at positions i,j, while * ensuring elements at i and j exist; if not, nothing * is done * * note: this method is included for future classes that extend * this class; 'protected' keyword used, acts as 'private' for security, * but acts as 'public' for inheritance */ protected void swap(int i, int j) { double temp = 0.0; // temp. swap variable //------------------------------------------------------ // if i and j are valid indicies if ((i < length()) && (j < length()) ) { temp = values[i]; // store element i values[i] = values[j]; // copy element j to i values[j] = temp; // copy original element at i } }// end of swap() /********************************************************** * toString() - returns a String of a formatted display of the array */ public String toString() { String result = ""; // result String that is returned //------------------------------------------------------ if (length() == 0) // if list is empty result = "empty list."; else // list has at least one element { for (int i=0; i-->