hamid_c
hamid_c

Reputation: 849

Why is this array constructed twice?

I've just started to learn Java - my learning source is: Deitel Java: How to Program.

Please look at this piece of code:

1    public class DeckOfCards
2    {
3       private Card[] deck; // array of Card objects
4       private int currentCard; // index of next Card to be dealt (0-51)
5       private static final int NUMBER_OF_CARDS = 52; // constant # of Cards
6       // random number generator
7       private static final Random randomNumbers = new Random();
8   
9     // constructor fills deck of Cards
10    public DeckOfCards()
11  {
12   String[] faces = { "Ace", "Deuce", "Three", "Four", "Five", "Six", 
13    "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
14          String[] suits = { "Hearts", "Diamonds", "Clubs", "Spades" };
15    
16          deck = new Card[ NUMBER_OF_CARDS ]; // create array of Card objects
17          currentCard = 0; // set currentCard so first Card dealt is deck[ 0 ]
18    
19          // populate deck with Card objects
20          for ( int count = 0; count < deck.length; count++ ) 
21             deck[ count ] = 
22                new Card( faces[ count % 13 ], suits[ count / 13 ] );
23       } // end DeckOfCards constructor

My problem is that I can't understand why the program constructs the deck array twice?

The first time (line 16), and it's okay as the program describes it (create array of card objects), but why for the second time (line 21-22)? Wouldn't a programmer simply use this expression without using new, like this:

deck[ count ] = Card( faces[ count % 13 ], suits[ count / 13 ] );

and this is the Card class if required:

public class Card 
{
   private String face; // face of card ("Ace", "Deuce", ...)
   private String suit; // suit of card ("Hearts", "Diamonds", ...)

   // two-argument constructor initializes card's face and suit
   public Card( String cardFace, String cardSuit )
   {
      face = cardFace; // initialize face of card
      suit = cardSuit; // initialize suit of card
   } // end two-argument Card constructor

   // return String representation of Card
   public String toString() 
   { 
      return face + " of " + suit;
   } // end method toString
} // end class Card

Upvotes: 0

Views: 488

Answers (4)

user3248346
user3248346

Reputation:

No. The array is not being created twice. It is being created once and then it is being populated.

In this statement, the programmer is creating an array and assigning it to a reference called deck:

 deck = new Card[ NUMBER_OF_CARDS ];

In the following statement, the programmer is assigning values to each component in the array:

deck[ count ] = new Card( faces[ count % 13 ], suits[ count / 13 ] );

This array has components of type Card and in this last statement the programmer is creating objects of type Card and assigning them to each component in the array.

Upvotes: 3

Alboz
Alboz

Reputation: 1851

The second time is not constructing the array but instead is populating it with Card objects.

On line 16 it instantiated an array but it's objects have all the default value for an object > null.

deck[ count ] = Card( faces[ count % 13 ], suits[ count / 13 ] );

This line creates a new Card object and the reference to this newly created object is stored in the deck[count] array cell.

Upvotes: 0

Arlind Hajredinaj
Arlind Hajredinaj

Reputation: 8519

On line 16 the "programmer" which is actually the author of the book,

deck = new Card[ NUMBER_OF_CARDS ];

is initializing an array with a length of the value of the static variable NUMBER_OF_CARDS which is 52. On line 21 and 22 the author is not initializing the array but is initializing the elements inside the array which are Objects of type Card.

deck[ count ] = new Card( faces[ count % 13 ], suits[ count / 13 ] );

ps I love that book

Upvotes: 0

sol4me
sol4me

Reputation: 15698

We are only creating array once. First time we are declaring an array but each individual entry inside array is null and second time we are assigning each entry inside array to refer a card object.

Upvotes: 0

Related Questions