Programming and Problem Solving with Java

(やまだぃちぅ) #1
621

Summary


Two-dimensional arrays are useful for processing information that is represented
naturally in tabular form. Processing data in two-dimensional arrays usually takes
one of two forms: processing by row or processing by column. Java implements a
two-dimensional array as an array of references to one-dimensional arrays.
Associated with each two-dimensional array is a final instance variable lengththat
contains the number of rows. Associated with each row of the table is a final
instance variable lengththat contains the number of items in the row (the column
length). The number of items in a row is usually the same for each row, but does not
need to be. If the rows are uneven, the array is called a ragged array.
A multidimensional array is a collection of like components that are ordered on
more than two dimensions. Each component is accessed by a set of indexes, one for
each dimension, that represents the component’s position on the various
dimensions. Each index may be thought of as describing a feature of a given array
component.
The floating-point types built into the Java language are floatand double.
Floating-point numbers are represented in the computer with a fraction and an
exponent. This representation permits numbers that are much larger or much
smaller than those that can be represented with the integral types. Floating-point
representation also allows us to perform calculations on numbers with fractional
parts.
Using floating-point numbers in arithmetic calculations does have some
drawbacks. Representational errors, for example, can affect the accuracy of compu-
tations. When using floating-point numbers, keep in mind that if two numbers are
vastly different from each other in size, adding or subtracting them can produce the
wrong answer. Remember, also, that the computer has a limited range of numbers
that it can represent. If your code tries to compute a value that is too large or too
small, it may result in unusual or unexpected values.
The class DecimalFormatprovides methods that allow the user to specify the
appearance of numeric output.


Quick Check


1.Declare a two-dimensional array named plan, and create an array object with 30
rows and 10 columns. The component type of the array is float. (pp. 582–583)
2.Given the array created in Question 1, answer the following questions.
a.Assign the value 27.3 to the component in row 13, column 7 of the array plan
from Question 1. (pp. 583–584)
b.We can use nestedforloops to sum the values in each row of the arrayplan.
What range of values would the outerforloop count through to do this?
(pp. 587–589)
Free download pdf