Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^512) | One-Dimensional Arrays
Testing and Debugging Hints
1.When an individual component of a one-dimensional array is accessed, the
index must be within the range 0 through the array size minus 1. Attempting
to use an index value outside this range will cause your application to crash.
2.The individual components of an array are themselves variables of the com-
ponent type. When values are stored into an array, they should either be of
the component type or be explicitly converted to the component type; other-
wise, implicit type conversion occurs.
3.As with all of Java’s composite data types, declaring an array variable and in-
stantiating the array object are separate steps. We omit the size of a one-
dimensional array in its declaration but mustspecify it when the array object
is instantiated.
4.When an array is an argument, the reference to the array object is passed to
the method. The method cannot change the reference, but it can change the
elements in the array.
5.An individual array component can be passed as an argument. If the compo-
nent is of a reference type, the method can change the referenced object if it
is a mutable object. If the component is of an atomic type, the method cannot
change it.
6.Although an object of a reference type passed as an argument can be changed
if the type is mutable, it is poor programming style to do so.
7.We use subarray processing to process array components when the actual
number of data items is not known until the application begins executing.
The lengthfield of the array object contains the number of slots in the array;
the number of data values stored into the array may differ.
8.When methods perform subarray processing on a one-dimensional array, the
array name and the number of data items actually stored in the array should
be encapsulated together into a class.
9.When a one-dimensional array is instantiated without an initializer list, each
of the values is automatically initialized to its default value.
10.A one-dimensional array is an object, so a reference to it may be set to null.
11.When processing the components in a one-dimensional array, we use a loop
that begins at zero and stops when the counter is equal to the lengthfield as-
sociated with the array object.

Free download pdf