teaching machines

CS 1: Lecture 29 – Growable Arrays

November 15, 2017 by . Filed under cs1, cs145, cs148, fall 2017, lectures.

Dear students,

Arrays make the modern world happen. The computer itself can be viewed as one big array, lining up all our data, music, photos, and movies on disks and in RAM. However, arrays have their limits. In particular, arrays are fixed in size. We need to know their size when we create them. Sometimes the size is not information we have. Adding something to an array is not possible. It’s also awkward to remove something from an array. We might null or zero out an element, but its shadow is still there. The length property will report the same number throughout the array’s lifetime.

Enter ArrayList. It wraps around an array in Java and makes it feel resizable. Internally, it keeps an array of some fixed size. When it fills up, it creates a new array that’s bigger, copies over the data, and ditches the old array.

For the most part, we can treat arrays and ArrayLists the same. Their interface is slightly different. The following table contrasts their operations:

operation array ArrayList
create type[] id = new type[#]; ArrayList<Type> id = new ArrayList<Type>();
get count id.length id.size()
read item id[i] id.get(i)
write item id[i] = expr; id.set(i, expr)
append item not supported id.add(item)
remove item not supported id.remove(item) or id.remove(index)

Let’s see ArrayLists in action through the following exercises:

We’ll solve the second of these through a Monte Carlo simulation. Such a simulation can be summarized as follows: simulate a phenomenon n times and compute your expected value of some measure of interest as the average across all runs. Here’s an interesting historical note about the first formal description of the Monte Carlo simulation from physicist Stanislaw Ulam:

The first thoughts and attempts I made to practice [the Monte Carlo Method] were suggested by a question which occurred to me in 1946 as I was convalescing from an illness and playing solitaires. The question was what are the chances that a Canfield solitaire laid out with 52 cards will come out successfully? After spending a lot of time trying to estimate them by pure combinatorial calculations, I wondered whether a more practical method than “abstract thinking” might not be to lay it out say one hundred times and simply observe and count the number of successful plays. This was already possible to envisage with the beginning of the new era of fast computers, and I immediately thought of problems of neutron diffusion and other questions of mathematical physics, and more generally how to change processes described by certain differential equations into an equivalent form interpretable as a succession of random operations. Later [in 1946], I described the idea to John von Neumann, and we began to plan actual calculations.

Ulam and von Neumann used the ENIAC, one of the first programmable computers, to simulate various physical phenomena for the Manhattan Project and the hydrogen bomb. We’ll just use it for gambling.

We’re going to write a few helper methods to support this:

Here’s your TODO to complete before we meet again:

See you next class!

Sincerely,

P.S. It’s Haiku Friday!

After a big meal
I move to a new body
People think it’s growth

P.P.S. Here’s the code we wrote together…

Table.java

package lecture1115;

import java.util.Arrays;

public class Table {
  public static void main(String[] args) {
//    System.out.println(Arrays.deepToString(timesTable(5, 4)));
    int[][] table = timesTable(5, 4);
    
    System.out.print("   ");
    for (int c = 0; c < table[0].length; ++c) {
      System.out.printf("%3d", c);
    }
    System.out.println();
    
    for (int r = 0; r < table.length; ++r) {
      System.out.printf("%3d", r);
      for (int c = 0; c < table[r].length; ++c) {
        System.out.printf("%3d", table[r][c]);
      }
      System.out.println();
    }
  }
  
  private static int[][] timesTable(int w, int h) {
    int[][] table = new int[h][w];
    
    for (int r = 0; r < table.length; ++r) {
      for (int c = 0; c < table[r].length; ++c) {
        table[r][c] = c * r;
      }
    }
    
    return table;
  }
}

Suffix.java

package lecture1115;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Suffix {
  public static void main(String[] args) throws FileNotFoundException {
    ArrayList<String> words = new ArrayList<>();
    Scanner dictionary = new Scanner(new File("/usr/share/dict/words"));
    while (dictionary.hasNextLine()) {
      words.add(dictionary.nextLine());
    }
    dictionary.close();
    
    String suffix = "phobia";
    for (int i = 0; i < words.size(); ++i) {
      if (words.get(i).endsWith(suffix)) {
        System.out.println(words.get(i));
      }
    }
  }
}

MonteCarlo.java

package lecture1115;

import java.util.ArrayList;
import java.util.Random;

public class MonteCarlo {
  public static void main(String[] args) {
    ArrayList<String> deck1 = getDeck();
    ArrayList<String> deck2 = getDeck();
    shuffle(deck1);
    System.out.println(deck1);
  }
  
  public static void shuffle(ArrayList<String> deck) {
    Random g = new Random();
    for (int i = deck.size() - 1; i >= 0; --i) {
      int j = g.nextInt(i + 1);
      String tmp = deck.get(i);
      deck.set(i, deck.get(j));
      deck.set(j, tmp);
    }
  }
  
  public static ArrayList<String> getDeck() {
    String[] suits = {"\u2665", "\u2666", "\u2660", "\u2663"};
    String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    
    ArrayList<String> deck = new ArrayList<String>();
    
    for (String suit : suits) {
      for (String rank : ranks) {
        deck.add(rank + suit);
      }
    }
    
    return deck;
  }
}