简体   繁体   中英

Why is the same loop used twice & Why is the value of of a array decreased?

I am following the offical java tutorial and i want to know why the for loop is repeated in both classes.

public class Deck {

    public static int numSuits = 4;
    public static int numRanks = 13;
    public static int numCards = numSuits * numRanks;

    private Card[][] cards;

    public Deck() {
        cards = new Card[numSuits][numRanks];
        for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) {
            for (int rank = Card.ACE; rank <= Card.KING; rank++) {
                //Don't understand why rank is decreased by one
                cards[suit-1][rank-1] = new Card(rank, suit);
            }
        }
    }

    public Card getCard(int suit, int rank) {
        return cards[suit-1][rank-1];
    }
}


      public class DisplayDeck {
    public static void main(String[] args) {
        Deck deck = new Deck();
        for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) {
            for (int rank = Card.ACE; rank <= Card.KING; rank++) {
                Card card = deck.getCard(suit, rank);
                System.out.format("%s of %s%n",
                    card.rankToString(card.getRank()),
                    card.suitToString(card.getSuit()));
            }
        }
    }
}

also the code for the cards class can be found here also can anyone tell me why the array values in the deck constructor is decreased by one?, since the intial value is one wouldn't this just cause and error?. thanks.

The loops are repeated twice because their bodies are different:

  • The loop inside the Deck() constructor initializes the cards
  • The loop inside main prints the cards.

The two methods do it in the same order, that's why the headers of the loops are identical.

The numbers are adjusted by one because in Java (as well as in C, C++, C#, and many other languages) array indexes are numbered from zero, rather than being numbered from one. The constants in the cards program, on the other hand, are one-based, so you must subtract one from them to get the numbers aligned with the way the arrays are indexed.

I don't know what pair of loops you're talking about, but i'll address what I can.

The first loop group

    for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) {
        for (int rank = Card.ACE; rank <= Card.KING; rank++) {
            //Don't understand why rank is decreased by one
            cards[suit-1][rank-1] = new Card(rank, suit);
        }
    }

builds the deck. the outer loop iterates over the suits, and the inner loop iterates over values. the result is the full 52 card deck. 13 values per suit.


the second loop group:

    for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) {
        for (int rank = Card.ACE; rank <= Card.KING; rank++) {
            Card card = deck.getCard(suit, rank);
            System.out.format("%s of %s%n",
                card.rankToString(card.getRank()),
                card.suitToString(card.getSuit()));
        }
    }

iterates in the same manner as the first. suits in the outer-loop and values in the inner-loop. But this time, it's not actually building the deck, rather, it's printing the results of what's in the deck.

in both cases, the reason is because you have a 2 dimensional array. the outer array is the suits, the inner array is the ranks. using arrays, you have to use different counting variables to go through them. for example, you want the outer loop to run 4 times, but you want the inner loop to run 13 times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM