Programming and Problem Solving with Java

(やまだぃちぅ) #1
513

Summary


The one-dimensional array is a homogeneous data structure that gives a name to a
sequential group of like components. Each component is accessed by its relative po-
sition within the group (rather than by its name, as in a class), and each component
is a variable of the component type. To access a particular component, we give the
name of the array and an index that specifies which component of the group we
want. The index can be an expression of any integral type except long, as long as it
evaluates to an integer ranging from 0 through the array size minus 1. Array compo-
nents can be accessed in random order directly, or they can be accessed sequentially
by stepping through the index values one at a time.


Quick Check


1.Declare and instantiate a one-dimensional array named quizAnswerthat
contains 12 components indexed by the integers 0 through 11. The component
type is boolean. (pp. 486–488)
2.Given the declarations
final intSIZE = 30 ;
char[] firstName = newchar[SIZE];
a.Write an assignment statement that stores ‘A’ into the first component of
the array firstName. (pp. 488–490)
b.Write an output statement that prints the value of the fourteenth
component of the array firstName. (pp. 488–490)
c.Write a forstatement that fills the array firstNamewith blanks. (p. 491)
3.Declare and instantiate a five-element one-dimensionalintarray named
oddNums, using an initializer list that contains the first five odd integers, starting
with 1. (p. 488)
4.Give the heading for a public void method named someFunc, where someFunchas
a single parameter: a one-dimensional floatarray called values. (pp. 500–501)
5.Given the declaration
studentRec[] gradeBook = newstudentRec[ 150 ];
where StudentRecis the class defined on page 511 in this chapter, do the
following:
a.Write an assignment statement that records the fact that the tenth student
has a grade point average of 3.25. (p. 500)
b.Write an assignment statement that records the fact that the fourth student
scored 78 on the third exam. (p. 500)
Free download pdf