Deprecated: Creation of dynamic property wpdb::$categories is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$post2cat is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$link2cat is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Return type of WP_Theme::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 554

Deprecated: Return type of WP_Theme::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 595

Deprecated: Return type of WP_Theme::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 535

Deprecated: Return type of WP_Theme::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 544

Deprecated: Return type of WP_REST_Request::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 960

Deprecated: Return type of WP_REST_Request::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 980

Deprecated: Return type of WP_REST_Request::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 992

Deprecated: Return type of WP_REST_Request::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 1003

Deprecated: Return type of WP_Block_List::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 151

Deprecated: Return type of WP_Block_List::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 175

Deprecated: Return type of WP_Block_List::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 164

Deprecated: Return type of WP_Block_List::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 186

Deprecated: Return type of WP_Block_List::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 138

Deprecated: Return type of WP_Block_List::offsetExists($index) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 75

Deprecated: Return type of WP_Block_List::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 89

Deprecated: Return type of WP_Block_List::offsetSet($index, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 110

Deprecated: Return type of WP_Block_List::offsetUnset($index) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 127

Deprecated: Return type of WP_Block_List::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 199

Deprecated: Creation of dynamic property Automattic\Jetpack\Connection\Manager::$error_handler is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-connection/src/class-manager.php on line 91

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Instagram_Gallery::$is_wpcom is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-instagram-gallery.php on line 23

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Mailchimp::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php on line 17

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Tweetstorm_Gather::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php on line 21

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Tweetstorm_Gather::$is_wpcom is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php on line 22

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Gutenberg_Available_Extensions::$wpcom_is_site_specific_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/gutenberg-available-extensions.php on line 20

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Memberships::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php on line 23

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Memberships::$wpcom_is_site_specific_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php on line 24

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Subscribers::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/subscribers.php on line 15

Deprecated: Creation of dynamic property Automattic\Jetpack\Sync\Queue::$random_int is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-sync/src/class-queue.php on line 38

Deprecated: Creation of dynamic property Automattic\Jetpack\Sync\Queue::$random_int is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-sync/src/class-queue.php on line 38

Deprecated: Creation of dynamic property WPCOM_Widget_Goodreads::$shelves is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/modules/widgets/goodreads.php on line 33

Deprecated: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated in /var/www/twodee/blog/wp-includes/script-loader.php on line 348

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_classic_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 347

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 349

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block_side is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 350

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$panels_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 351

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$used_block_buttons is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 354

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198
» CS1: Lecture 30 – 2D Arrays and ArrayList

teaching machines

CS1: Lecture 30 – 2D Arrays and ArrayList

November 11, 2019 by . Filed under
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383

Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383

Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
cs1, fall 2019, lectures.

Dear students,

Today we will complete a whirlwind tour through a couple of concepts that really deserve a couple of conversations each. These concepts are two-dimensional arrays and ArrayList.

Two-dimensional Arrays

Let’s consider a famous statistical problem. Suppose birthdays are uniformly distributed across the calendar year. What’s the probability that we in this room all have different birthdays? High or low? How many people need to be in a room before we’re more likely to see at least one repeated birthday? The answer’s 22 and some change. We won’t do the statistics, but let’s do see if our class upholds the statistical prediction.

I have this file of our birthdays:

8 2
7 21
7 10
5 4
1 22
12 18
6 19
1 7
10 9
5 12
4 7
3 29
7 20
10 7
5 12
1 7
2 13
8 26
12 27
9 22
7 14
2 28
5 7
6 19
11 10
3 26
4 2
3 4
9 8
3 8
6 23
11 13
4 13
6 28
7 14
2 19
3 31
7 30
6 20
10 15
12 7
6 27
8 12
12 19
6 24
7 22
8 26
7 26
12 20
4 23
12 19
9 19
2 19
4 4
9 11
8 15
12 13
2 28
3 26
11 19
7 23
6 7
11 9
1 3
12 8
8 23
3 12
10 30
5 24
5 28
3 20
2 13
11 15
8 14
7 15
12 28
6 10
10 26
10 15
5 26
4 10
11 30
5 7
3 12
5 25
12 14
7 17
10 25
8 21
1 3
7 8
6 5
10 9
4 19
8 10
11 30
8 23
5 22
11 28
2 19
9 11
12 17
5 15
5 15
9 12
5 23
4 24
9 1
7 6
11 6
12 23
3 9
1 2
5 8
3 16

Let’s look for repeats. We start with a loop that just reads the input:

Scanner in = new Scanner(new File("birthdays.csv"));

while (in.hasNextInt()) {
  int month = in.nextInt();
  int day = in.nextInt();
}

in.close();

How can figure out if any of these birthdays has been seen before? Let’s keep counters. Everytime we see a birthday, we’ll increment that day’s counter. If any days have 2 or more birthdays, we have a repeat. Here we go:

int[] counts = new int[366];

Great. Maybe. To index into that thing, we will need to turn each day into an index in [0, 365]. This is not impossible, but I think it’s too much work. Remember that pattern we saw for making an array:

type[] name = new type[#];

It turns out that type can itself be an array type—meaning that we can have an array where each element is itself an array. In other words, we can have an array of arrays. The declaration for such an array looks like this:

int[][] counts = new int[12][31];

This means we’ll have 12 elements in the outer array, and each of those elements will be an array of 31 elements. We can reach into our array with two indices:

counts[0][0] = 17;   // 17 people born on January 1
counts[11][30] = 3;  // 3 people born on December 31

Watch the order of your indices. The outer array spans the months 0 through 11. The inner arrays span the days 0 through 30 within a month. Not every month has 31 days, but there’s no harm in asking for a few more ints that we’ll never use. We can integrate our array into our loop like so:

Scanner in = new Scanner(new File("birthdays.csv"));

int[][] counts = new int[12][31];
while (in.hasNextInt()) {
  int month = in.nextInt();
  int day = in.nextInt();
  ++counts[month - 1][day - 1];
}

in.close();

Okay, we’ve got our tallies. Are there repeats? We’ll have to walk back through the array of arrays to look for them. We could hardcode 12 as the upperbound of our outer index, but it’s safer to ask the array itself how many months there are:

for (int iMonth = 0; iMonth < counts.length; ++iMonth) {
  // counts[iMonth] is the tallies for this month
}

Within this loop, we can visit all of the days. Once again, we ask the array how many days it has in order to get our upper bound:

for (int iMonth = 0; iMonth < counts.length; ++iMonth) {
  for (int iDay = 0; iDay < counts[iMonth].length; ++iDay) {
    // counts[iMonth][iDay] is the tally
  }
}

And finally, we ask which days have more than one person. Let’s also spell out the months by indexing into a helper array:

String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
for (int iMonth = 0; iMonth < counts.length; ++iMonth) {
  for (int iDay = 0; iDay < counts[iMonth].length; ++iDay) {
    if (counts[iMonth][iDay] > 1) {
      System.out.printf("%s %d saw %d births.%n", monthNames[iMonth], iDay + 1, counts[iMonth][iDay]);
    }
  }
}

Growable Arrays

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)

We’ll explore the use of ArrayList in the context of simulating the card game War.

TODO

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

See you next class!

Sincerely,

P.S. It’s time for a haiku!

2D is tiny
3D’s thicker, but 4D?
We’ll run out of space

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

Birthdays.java

package lecture1111.cs145;

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

public class Birthdays {
  public static void main(String[] args) throws FileNotFoundException {
    Scanner in = new Scanner(new File("/Users/johnch/Desktop/birthdays.csv"));

    int[][] tallies = new int[12][31];

    while (in.hasNextInt()) {
      int month = in.nextInt();
      int day = in.nextInt();

      tallies[month - 1][day - 1]++;
    }

    for (int m = 0; m < tallies.length; ++m) {
      // I have a month row in hand with tallies[m]
      for (int d = 0; d < tallies[m].length; ++d) {
//        System.out.printf("%3d", tallies[m][d]);
        if (tallies[m][d] > 1) {
          System.out.printf("%d %d -> %d%n", m + 1, d + 1, tallies[m][d]);
        }
      }
      System.out.println();
    }

    in.close();
  }
}

War.java

package lecture1111.cs145;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

public class War {
  public static void main(String[] args) {
    ArrayList<Integer> deck1 = getDeck();
    ArrayList<Integer> deck2 = getDeck();

    Collections.shuffle(deck1);
    Collections.shuffle(deck2);

    while (!deck1.isEmpty() && !deck2.isEmpty()) {
      int card1 = deck1.remove(0);
      int card2 = deck2.remove(0);

      if (card1 > card2) {
        deck1.add(card1);
        deck1.add(card2);
      } else if (card2 > card1) {
        deck2.add(card1);
        deck2.add(card2);
      } else {
        deck1.add(card1);
        deck2.add(card2);
      }

      System.out.printf("%d vs. %d%n", deck1.size(), deck2.size());
    }
  }

  public static ArrayList<Integer> getDeck() {
    ArrayList<Integer> deck = new ArrayList<Integer>();

    for (int i = 2; i <= 14; i++) {
      deck.add(i);
      deck.add(i);
      deck.add(i);
      deck.add(i);
    }

    return deck;
  }
}

Birthdays.java

package lecture1111.cs148;

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

public class Birthdays {
  public static void main(String[] args) throws FileNotFoundException {
    File file = new File("/Users/johnch/Desktop/birthdays.csv");
    Scanner in = new Scanner(file);

    int[][] tallies = new int[12][31];

    while (in.hasNextInt()) {
      int month = in.nextInt();
      int day = in.nextInt();
      tallies[month - 1][day - 1]++;
    }

    for (int m = 0; m < tallies.length; m++) {
      // tallies[m] is a month's array
      for (int d = 0; d < tallies[m].length; d++) {
        if (tallies[m][d] > 1) {
          System.out.printf("%d %d -> %2d%n", m + 1, d + 1, tallies[m][d]);
        }
      }
    }

    in.close();
  }
}

War.java

package lecture1111.cs148;

import java.util.ArrayList;
import java.util.Collections;

public class War {
  public static void main(String[] args) {
    ArrayList<Integer> deck = getDeck();
    Collections.shuffle(deck);
    System.out.println(deck);
  }

  public static ArrayList<Integer> getDeck() {
    ArrayList<Integer> deck = new ArrayList<Integer>();

    for (int i = 1; i <= 13; i++) {
      deck.add(i);
      deck.add(i);
      deck.add(i);
      deck.add(i);
    }

    return deck;
  }
}