Skip to main content
Engineering LibreTexts

3.6: Arrays of Cards

  • Page ID
    15289
  • Just as you can create an array of String objects, you can create an array of Card objects. The following statement creates an array of 52 cards:

    Card[] cards = new Card[52];
    

    Figure 12.6.1 shows the state diagram for this array.

    State diagram of an unpopulated Card array.
    Figure \(\PageIndex{1}\): State diagram of an unpopulated Card array.

    Although we call it an “array of cards”, the array contains references to objects; it does not contain the Card objects themselves. The elements are initialized to null. You can access the elements of the array in the usual way:

    if (cards[0] == null) {
        System.out.println("No card yet!");
    }
    

    But if you try to access the instance variables of the non-existent Cards, you will get a NullPointerException.

    cards[0].rank      // NullPointerException
    

    That code won’t work until we put cards in the array. One way to populate the array is to write nested for loops:

    int index = 0;
    for (int suit = 0; suit <= 3; suit++) {
        for (int rank = 1; rank <= 13; rank++) {
            cards[index] = new Card(rank, suit);
            index++;
        }
    }
    

    The outer loop iterates suits from 0 to 3. For each suit, the inner loop iterates ranks from 1 to 13. Since the outer loop runs 4 times, and the inner loop runs 13 times for each suit, the body is executed 52 times.

    We use a separate variable index to keep track of where in the array the next card should go. Figure 12.6.2 shows what the array looks like after the first two cards have been created.

    State diagram of a Card array with two cards.
    Figure \(\PageIndex{2}\): State diagram of a Card array with two cards.

    When you work with arrays, it is convenient to have a method that displays the contents. We have seen the pattern for traversing an array several times, so the following method should be familiar:

    public static void printDeck(Card[] cards) {
        for (int i = 0; i < cards.length; i++) {
            System.out.println(cards[i]);
        }
    }
    

    Since cards has type Card[], an element of cards has type Card. So println invokes the toString method in the Card class. This method is similar to invoking System.out.println(Arrays.toString(cards)).