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
» CS 145 Lecture 30 – ArrayList

teaching machines

CS 145 Lecture 30 – ArrayList

November 18, 2016 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
cs145, fall 2016, 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. Just like you wrap around a pair of shoes.

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 last 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 to for gambling.

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

See you next class!

Sincerely,

P.S. It’s Haiku Friday!

I wanted three kids
She two, so just to be safe…
We bought a schoolbus

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

Suffixer.java

package lecture1118;

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

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

Cards.java

package lecture1118;

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

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

// deck1.remove(51);
// deck1.remove(38);
// deck1.remove(25);
// deck1.remove(12);

    shuffle(deck1);

    ArrayList<String> deck2 = getDeck();
    shuffle(deck2);

// System.out.println(deck);

    int i = 0;
    while (deck1.size() > 0 && deck2.size() > 0) {
      System.out.printf("%d vs %d%n", deck1.size(), deck2.size());
      playRound(deck1, deck2);
      ++i;
      if (i > 100) {
        i = 0;
        shuffle(deck1);
        shuffle(deck2);
      }
    }
  }

  private static void playRound(ArrayList<String> deck1, ArrayList<String> deck2) {
    String card1 = deck1.remove(0);
    String card2 = deck2.remove(0);
    if (getWorth(card1) > getWorth(card2)) {
      deck1.add(card1);
      deck1.add(card2);
    } else if (getWorth(card1) < getWorth(card2)) {
      deck2.add(card1);
      deck2.add(card2);
    } else {
      deck1.add(card1);
      deck2.add(card2);
//      ArrayList<String> pile = new ArrayList<String>();
//      pile.add(card1);
//      pile.add(card2);
//
//      while (deck1.size() > 0 && deck2.size() > 0 && getWorth(card1) == getWorth(card2)) {
//        card1 = deck1.remove(0);
//        card2 = deck2.remove(0);
//        pile.add(card1);
//        pile.add(card2);
//      }
//
//      if (deck1.size() == 0 && deck2.size() == 0) {
//        System.out.println("Tie!");
//      } else if (deck1.size() == 0) {
//        System.out.println("Player two wins!");
//      } else if (deck2.size() == 0) {
//        System.out.println("Player one wins!");
//      } else if (getWorth(card1) > getWorth(card2)) {
//        for (String card : pile) {
//          deck1.add(card);
//        }
//      } else if (getWorth(card1) < getWorth(card2)) {
//        for (String card : pile) {
//          deck2.add(card);
//        }
//      }
    }
  }

  private static void shuffle(ArrayList<String> deck) {
    Random generator = new Random();
    for (int i = deck.size() - 1; i > 0; --i) {
      int j = generator.nextInt(i + 1);
      String tmp = deck.get(i);
      deck.set(i, deck.get(j));
      deck.set(j, tmp);
    }
  }

  private static int getWorth(String cardLabel) {
    if (cardLabel.startsWith("A")) {
      return 1;
    } else if (cardLabel.startsWith("J")) {
      return 11;
    } else if (cardLabel.startsWith("Q")) {
      return 12;
    } else if (cardLabel.startsWith("K")) {
      return 13;
    } else {
      return Integer.parseInt(cardLabel.substring(0, cardLabel.length() - 1));
    }
  }

  private 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<>();

    for (String suit : suits) {
      for (String rank : ranks) {
        deck.add(rank + suit);
      }
    }

    return deck;
  }
}

War.java

package lecture1118;

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

public class War {
  public static void main(String[] args) {
    ArrayList<String> deck1 = getDeck();
    ArrayList<String> deck2 = getDeck();
    
    deck1.remove(51);
    deck1.remove(38);
    deck1.remove(25);
    deck1.remove(12);

    shuffle(deck1);
    shuffle(deck2);

    int i = 0;
    while (!deck1.isEmpty() && !deck2.isEmpty()) {
      System.out.println(deck1.size() + " vs. " + deck2.size());
      playOneRound(deck1, deck2);
      ++i;
      if (i > 100) {
        shuffle(deck1);
        shuffle(deck2);
        i = 0;
      }
    }
  }
  
  private static void playOneRound(ArrayList<String> deck1,
                                   ArrayList<String> deck2) {
    String card1 = deck1.remove(0);
    String card2 = deck2.remove(0);

    if (getWorth(card1) > getWorth(card2)) {
      deck1.add(card1);
      deck1.add(card2);
    } else if (getWorth(card1) < getWorth(card2)) {
      deck2.add(card1);
      deck2.add(card2);
    } else {
      deck1.add(card1);
      deck2.add(card2);
    }
  }
  
  private static int getWorth(String card) {
    if (card.startsWith("A")) {
      return 1;
    } else if (card.startsWith("J")) {
      return 11;
    } else if (card.startsWith("Q")) {
      return 12;
    } else if (card.startsWith("K")) {
      return 13;
    } else {
      return Integer.parseInt(card.substring(0, card.length() - 1));
    }
  }
  
  private static void shuffle(ArrayList<String> deck) {
    // for i from n−1 downto 1 do
    //   j ← random integer such that 0 ≤ j ≤ i
    //   exchange a[j] and a[i]
    
    Random generator = new Random();
    for (int i = deck.size() - 1; i >= 1; --i) {
      int j = generator.nextInt(i + 1);
      
      // tmp = a
      // a = b
      // b = tmp
      
      String tmp = deck.get(i);
      deck.set(i, deck.get(j));
      deck.set(j, tmp);
    }
  }

  private 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 (int si = 0; si < suits.length; ++si) {
      for (int ri = 0; ri < ranks.length; ++ri) {
        String card = ranks[ri] + suits[si];
        deck.add(card);
      }
    }
    
    return deck;
  }
}

Bet.java

package lecture1118;

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

public class Bet {
  public static void main(String[] args) {

    int earnings = 0;
    final int N = 10000000; 
    for (int i = 0; i < N; ++i) {
      ArrayList<String> deck1 = getDeck();
      ArrayList<String> deck2 = getDeck();
      shuffle(deck1);
      shuffle(deck2);
      int payoff = play(deck1, deck2);
      earnings += payoff;
    }
    
    System.out.println(earnings);
    System.out.println(earnings / (double) N);
  }
  
  private static int play(ArrayList<String> deck1,
                          ArrayList<String> deck2) {
    for (int i = 0; i < deck1.size(); ++i) {
      if (deck1.get(i).equals(deck2.get(i))) {
        return -5;
      }
    }
    return 5;
  }

  private static void shuffle(ArrayList<String> deck) {
    Random generator = new Random();
    for (int i = deck.size() - 1; i > 0; --i) {
      int j = generator.nextInt(i + 1);
      String tmp = deck.get(i);
      deck.set(i, deck.get(j));
      deck.set(j, tmp);
    }
  }

  private 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<>();

    for (String suit : suits) {
      for (String rank : ranks) {
        deck.add(rank + suit);
      }
    }

    return deck;
  }
}