Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^482) | One-Dimensional Arrays
Composite data types come in two forms: unstructured and structured. In an
unstructured data type, no relationship exists among the values in the data type other
than that they are members of the same collection. A structured data type, on the
other hand, is an organized collection of components in which a relationship exists
among the items in the collection. We use this relationship to access individual
items within the collection as well as to manipulate the collection as a whole.
A value in an atomic type is a single data item; it cannot be broken down into
component parts. For example, in Java each intvalue is a single integer number
and cannot be further decomposed. In contrast, in a composite data type, each
value is a collection of component items. The entire collection is given a single
name, yet each component can still be accessed individually.
The class is an example of a composite data type. A class has a name and is
composed of named data fields and methods. An instance of a class, including the
data fields and methods, can be passed as an argument. The data fields and methods can be
accessed individually by name. A class is unstructured because the meaning does not depend
on the ordering of the data fields or the methods within the source code. That is, we can
change the order in which the members of the class are listed without changing the func-
tion of the class.
In Java, all composite types are either classes, interfaces, or arrays. Rather than talking
about the typeof a composite object, we talk about its class. An example of a composite ob-
ject in Java is an instance of the Stringclass, used for creating and manipulating strings. When
you declare a variable myStringto be of class String,myStringdoes not reference just one
atomic data value; rather, it references an entire collection of characters and the methods
that manipulate the characters. Even so, you can access each component in the string indi-
vidually by using an expression such as myString.charAt(3), which accesses the charvalue at
position 3. Therefore the characters within the string are ordered.
Atomic data types serve as the building blocks for composite types. A composite type
gathers together a set of component values and usually imposes a specific arrangement
on them (see Figure 10.2). If the compos-
ite type is a built-in type, the syntax of
the language provides the accessing
mechanism. If the composite type is user-
defined, the accessing mechanism is built
into the methods provided with the class.
In Chapters 1 through 9, we have dis-
cussed control structures and the class,
an unstructured composite type. In the
next three chapters, we focus on struc-
tured composite data types. Of course, we
do not abandon the class, but we focus on
having a structured composite type as a
field in a class.
Unstructured data type A col-
lection of components that are
not organized with respect to
one another.
Structured data type An or-
ganized collection of
components; the organization
determines the means used to
access individual components
:
Atomic UnstructuredComposite CompositeStructured
Figure 10.2 Atomic (Simple) and Composite Data Types

Free download pdf