System, but they may not be reproduced for publication



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

 Chapter 4 Self Test
1.
What is the difference between a class and an object?
2.
How is a class defined?
3.
What does each object have its own copy of?
4.
Using two separate statements, show how to declare an object called counter of a
class called MyCounter.
5.
Show how a method called myMeth( ) is declared if it has a return type of double
and has two int parameters called a and b.
6.
How must a method return if it returns a value?
7.
What name does a constructor have?
8.
What does new do?
9.
What is garbage collection, and how does it work?
10.
What is this?
11.
Can a constructor have one or more parameters?
12.
If a method returns no value, what must its return type be?


Chapter 5
More Data Types and Operators
Key Skills & Concepts

Understand and create arrays

Create multidimensional arrays

Create irregular arrays

Know the alternative array declaration syntax

Assign array references
History
Topics
Tutorials
Offers & Deals
Highlights
Settings
Support
Sign Out


T

Use the length array member

Use the for­each style for loop

Work with strings

Apply command­line arguments

Use type inference with local variables

Use the bitwise operators

Apply the ? operator
his chapter returns to the subject of Java’s data types and operators. It discusses arrays,
the String type, local variable type inference, the bitwise operators, and the ? ternary
operator. It also covers Java’s for­each style for loop. Along the way, command­line
arguments are described.
ARRAYS
An array is a collection of variables of the same type, referred to by a common name. In
Java, arrays can have one or more dimensions, although the one­dimensional array is
the most common. Arrays are used for a variety of purposes because they offer a
convenient means of grouping together related variables. For example, you might use
an array to hold a record of the daily high temperature for a month, a list of stock price
averages, or a list of your collection of programming books.
The principal advantage of an array is that it organizes data in such a way that it can be
easily manipulated. For example, if you have an array containing the incomes for a
selected group of households, it is easy to compute the average income by cycling
through the array. Also, arrays organize data in such a way that it can be easily sorted.
Although arrays in Java can be used just like arrays in other programming languages,
they have one special attribute: they are implemented as objects. This fact is one reason
that a discussion of arrays was deferred until objects had been introduced. By
implementing arrays as objects, several important advantages are gained, not the least
of which is that unused arrays can be garbage collected.
One-Dimensional Arrays


A one­dimensional array is a list of related variables. Such lists are common in
programming. For example, you might use a one­dimensional array to store the
account numbers of the active users on a network. Another array might be used to store
the current batting averages for a baseball team.
To declare a one­dimensional array, you can use this general form:
type array­name[ ] = new type[size
Here, type declares the element type of the array. (The element type is also commonly
referred to as the base type.) The element type determines the data type of each
element contained in the array. The number of elements that the array will hold is
determined by size. Since arrays are implemented as objects, the creation of an array is
a two­step process. First, you declare an array reference variable. Second, you allocate
memory for the array, assigning a reference to that memory to the array variable. Thus,
arrays in Java are dynamically allocated using the new operator.
Here is an example. The following creates an int array of 10 elements and links it to an
array reference variable named sample:
This declaration works just like an object declaration. The sample variable holds a
reference to the memory allocated by new. This memory is large enough to hold 10
elements of type int. As with objects, it is possible to break the preceding declaration in
two. For example:
In this case, when sample is first created, it refers to no physical object. It is only after
the second statement executes that sample is linked with an array.
An individual element within an array is accessed by use of an index. An index
describes the position of an element within an array. In Java, all arrays have zero as the
index of their first element. Because sample has 10 elements, it has index values of 0
through 9. To index an array, specify the number of the element you want, surrounded
by square brackets. Thus, the first element in sample is sample[0], and the last
element is sample[9]. For example, the following program loads sample with the
numbers 0 through 9:


The output from the program is shown here:
Conceptually, the sample array looks like this:
Arrays are common in programming because they let you deal easily with large
numbers of related variables. For example, the following program finds the minimum
and maximum values stored in the nums array by cycling through the array using a
for loop:


The output from the program is shown here:
In the preceding program, the nums array was given values by hand, using 10 separate
assignment statements. Although perfectly correct, there is an easier way to accomplish
this. Arrays can be initialized when they are created. The general form for initializing a
one­dimensional array is shown here:

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   21   22   23   24   25   26   27   28   ...   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ə