Chapter 5
Introduction to Arrays
5.1 One-Dimensional Arrays ................................................................ 83
5.1.1 Deep versus Shallow Array Copy ............................................... 93
5.1.2 Deep versus Shallow Array Comparison ........................................ 95
5.2 The Trading Game Revisited ........................................................... 96
5.3 Two-Dimensional Arrays ............................................................... 99
5.4 Variable Argument Methods ............................................................ 102
5.5 Command Line Arguments ............................................................. 103
5.6 Summary ................................................................................ 103
5.7 Syntax .................................................................................. 103
5.8 Important Points ....................................................................... 105
5.9 Exercises ................................................................................ 105
5.10 Lab ...................................................................................... 106
5.11 Project .................................................................................. 107
So far, we have examined how variables are declared, initialized, and manipulated. Remem-
ber that space in main memory is allocated for every single variable. If we want to create
space in main memory for one hundred integers, then we need to declare one hundred dis-
tinct integer variables. Although this approach is certainly plausible, it is not very practical.
As an alternative, one can declare an array of one hundred integers in a single statement.
This makes the program shorter and easier to write and maintain. Moreover, Java allows
the creation of variable-sized arrays. In other words, we can use an array to allocate as
much main memory as we deem necessary during the execution of a program.
5.1 One-Dimensional Arrays
Yahtzee is a popular dice game. Players roll five dice and try to get different combina-
tions. If a player is not satisfied with her or his dice, then he or she is allowed to reroll some
or all of the dice up to two times. A player gets a Yahtzee when all five dice show the same
value.
We will next show how to create a simplified version of the game, where the only com-
bination that the player is interesting in achieving is Yahtzee. We will use a data-driven
approach to design our program. The reason is that the variables are the pillar of any pro-
gram. In the Yahtzee case, we need to create five variables for the five dice. The skeleton of
the program can look as shown below.
class Yahtzee {
static int d1 , d2 , d3 , d4 , d5 ; // th e fi v e di c e
public s ta tic void main( String [ ] args ){
...
}
...
}
83

Get Learning Java Through Games now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.