ArrayAndArrayList
CSSE 221 – Fundamentals of Software Development Honors
Fall 2009–2010

You will do this exercise by yourself, but be quick to ask questions of your instructor, student assistants and classmates as desired.

Goals and Examples

Practice and hence solidify your understanding of using arrays and ArrayList's:

  1. Declaring an array or ArrayList or (more generically) List. Examples:
        double[] data;
        Employee[] employees;
    
        ArrayList<Double> scores;
        ArrayList<Student> students;
    
        List<Double> scores;
        List<Student> students;
    
  2. Allocating space for an array or ArrayList. Examples (assuming declarations above):
        data = new double[50];
        employees = new Employee[numberOfEmployees];
    
        scores = new ArrayList<Double>();
        students = new ArrayList<Student>();
    
        // You also can declare and allocate space in a single statement:
        double[] moreData = new double[100];
        List<Student> moreStudents = new ArrayList<Student>();
              // Note the use of interface-type for the above declaration.
    
  3. Getting (accessing) elements in an array or ArrayList. Examples (assuming declarations above, and also see examples to the right):
        ... data[4] ...
        ... employees[k] ...
    
        ... scores.get(4) ...
        ... students.get(k) ...
    
  4. Setting (mutating) elements in an array or ArrayList. Examples (assuming declarations above, and also see examples to the right):
        data[4] = valueFromFile;
        employees[k] = new Employee(...);
    
        scores.set(4, scoreFromFile);
        students.set(k, new Student(...)) ...
    
        // Also can add elements to the end of an ArrayList, and more:
        students.add(new Student(...));
        students.remove(j); // Remove element at index j,
                            // shifting the elements behind it
    
  5. Initializing all elements of an array or ArrayList, using old-style loops:
        Dog[] dogs = new Dog[numberOfDogs];
    
        for (int k = 0; k < dogs.length; ++k) {
            dogs[k] = new Dog(...);
        }
    
        List<Cat> cats = new ArrayList<Cat>();
    
        for (int k = 0; k < numberOfCats; ++k) {
            cats.add(new Cat(...));
        }
    

You can see more examples of arrays and ArrayLists in the notes on:

More examples

  1. Counting array or ArrayList elements that satisfy a given property, using old-style loops:
        int count = 0;
        for (int k = 0; k < dogs.length; ++k) {
            if (... dogs[k] ...) {
                ++ count;
            }
        }
    
        int otherCount = 0;
        for (int k = 0; k < cats.size(); ++k) {
            if (... cats.get(k) ...) {
                ++ otherCount;
            }
        }
    
  2. Summing array or ArrayList elements, using NEW-style loops:
        double totalWeight = 0;
        for (Dog dog : dogs) {
            totalWeight += dog.weight();
        }
    
        int totalAge = 0;
        for (Cat cat : cats) {
            totalAge += cat.age();
        }
    
    Note: you can NOT use the enhanced for loop to modify   an element of an array or ArrayList.
  3. The “histogram loop pattern”:

    1. Declare an array or ArrayList that will hold histogram values 0 .. n, where events are numbered and n is the biggest event that can occur.
    2. Initialize all elements of the histogram to 0.
    3. Repeatedly:
      • Get one event (call it m) from which the histogram is to be built.
      • Increment the histogram count at index m.
    The array or ArrayList now holds the desired histogram.
  4. Two (or more) dimensional arrays or ArrayList's, as in these examples:
        Integer[m][n] matrix = new Integer[m][n];
        for (int j = 0; j < matrix.length; ++j) {
            for (int k = 0; k < matrix[k].length; ++k) {
                matrix[j][k] = ...
            }
        }
    
        List<List<Integer>> matrix = new ArrayList<List<Integer>>();
        for (int j = 0; j < m; ++j) {
            matrix.add(new ArrayList<Integer>());
            for (int k = 0; k < n; ++k) {
                matrix.get(j).add(...);
            }
        }
    
    The above examples show why arrays are often preferable to ArrayList's when two or more dimensions are required.

Get the project

Checkout the ArraysAndArrayLists project in Eclipse in the usual way:

  1. Go to the SVN Repository perspective.
  2. Expand your individual repository.
  3. Right-click on the ArrayAndArrayList project and select Checkout.
  4. After completing the Checkout, return to the Java perspective.

The Exercise

You will:

Implement the TODO's in the project from top to bottom, except jumping ahead to implement each method as it is used. For example:

Your instructor will demo this process.

Commit your project periodically, and for sure after you complete it.