System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə27/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   23   24   25   26   27   28   29   30   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

type name[ ][ ]...[ ] = new type[size1][size2]...[sizeN
For example, the following declaration creates a 4 × 10 × 3 three­dimensional integer
array.
Initializing Multidimensional Arrays
A multidimensional array can be initialized by enclosing each dimension’s initializer list
within its own set of curly braces. For example, the general form of array initialization
for a two­dimensional array is shown here:
type­specifier array_name[ ] [ ] = {
val, val, val, ..., val },
val, val, val, ..., val },
.
.
.
val, val, val, ..., val }

Here, val indicates an initialization value. Each inner block designates a row. Within
each row, the first value will be stored in the first position of the subarray, the second
value in the second position, and so on. Notice that commas separate the initializer
blocks and that a semicolon follows the closing }.
For example, the following program initializes an array called sqrs with the numbers 1
through 10 and their squares:


Here is the output from the program:
ALTERNATIVE ARRAY DECLARATION SYNTAX
There is a second form that can be used to declare an array:
type[ ] var­name;


Here, the square brackets follow the type specifier, not the name of the array variable.
For example, the following two declarations are equivalent:
The following declarations are also equivalent:
This alternative declaration form offers convenience when declaring several arrays at
the same time. For example,
This creates three array variables of type int. It is the same as writing
The alternative declaration form is also useful when specifying an array as a return type
for a method. For example,
This declares that someMeth( ) returns an array of type int.
Because both forms of array declarations are in widespread use, both are used in this
book.
ASSIGNING ARRAY REFERENCES
As with other objects, when you assign one array reference variable to another, you are
simply changing what object that variable refers to. You are not causing a copy of the
array to be made, nor are you causing the contents of one array to be copied to the
other. For example, consider this program:


The output from the program is shown here:
As the output shows, after the assignment of nums1 to nums2, both array reference
variables refer to the same object.
USING THE LENGTH MEMBER


Recall that in Java, arrays are implemented as objects. One benefit of this approach is
that each array has associated with it a length instance variable that contains the
number of elements that the array can hold. (In other words, length contains the size
of the array.) Here is a program that demonstrates this property:
This program displays the following output:
Pay special attention to the way length is used with the two­dimensional array table.


As explained, a two­dimensional array is an array of arrays. Thus, when the expression
is used, it obtains the number of arrays stored in table, which is 3 in this case. To obtain
the length of any individual array in table, you will use an expression such as this,
which, in this case, obtains the length of the first array.
One other thing to notice in LengthDemo is the way that list.length is used by the
for loops to govern the number of iterations that take place. Since each array carries
with it its own length, you can use this information rather than manually keeping track
of an array’s size. Keep in mind that the value of length has nothing to do with the
number of elements that are actually in use. It contains the number of elements that the
array is capable of holding.
The inclusion of the length member simplifies many algorithms by making certain
types of array operations easier—and safer—to perform. For example, the following
program uses length to copy one array to another while preventing an array overrun
and its attendant run­time exception.
Here, length helps perform two important functions. First, it is used to confirm that
the target array is large enough to hold the contents of the source array. Second, it
provides the termination condition of the for loop that performs the copy. Of course, in


this simple example, the sizes of the arrays are easily known, but this same approach
can be applied to a wide range of more challenging situations.
Try This 5­2
A Queue Class
As you may know, a data structure is a means of organizing data. The simplest data
structure is the array, which is a linear list that supports random access to its elements.
Arrays are often used as the underpinning for more sophisticated data structures, such
as stacks and queues. A stack is a list in which elements can be accessed in first­in, last­
out (FILO) order only. A queue is a list in which elements can be accessed in first­in,
first­out (FIFO) order only. Thus, a stack is like a stack of plates on a table—the first
down is the last to be used. A queue is like a line at a bank—the first in line is the first
served.
What makes data structures such as stacks and queues interesting is that they combine
storage for information with the methods that access that information. Thus, stacks
and queues are data engines in which storage and retrieval are provided by the data
structure itself, not manually by your program. Such a combination is, obviously, an
excellent choice for a class, and in this project you will create a simple queue class.
In general, queues support two basic operations: put and get. Each put operation
places a new element on the end of the queue. Each get operation retrieves the next
element from the front of the queue. Queue operations are consumptive: once an
element has been retrieved, it cannot be retrieved again. The queue can also become
full, if there is no space available to store an item, and it can become empty, if all of the
elements have been removed.
One last point: There are two basic types of queues—circular and noncircular. A

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   23   24   25   26   27   28   29   30   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə