Java Reference
In-Depth Information
SR 8.8 Explain the concept of array bounds checking. What happens when a
Java array is indexed with an invalid value?
SR 8.9 What is an off-by-one error? How does it relate to arrays?
SR 8.10 Write code that increments (by one) each element of an array of
integers named values .
SR 8.11 Write code that computes and prints the sum of the elements of an
array of integers named values .
SR 8.12 What does an array initializer list accomplish?
SR 8.13 Can an entire array be passed as a parameter? How is this
accomplished?
8.3 Arrays of Objects
In the previous examples in this chapter, we used arrays to store primitive types
such as integers and characters. Arrays can also store references to objects as ele-
ments. Fairly complex information management structures can be created using
only arrays and other objects. For example, an array could contain objects, and
each of those objects could consist of several variables and the methods that use
them. Those variables could themselves be arrays, and so on. The design of a
program should capitalize on the ability to combine these constructs to create the
most appropriate representation for the information.
Keep in mind that the array itself is an object. So it would be appropriate to
picture an array of int values called weight as follows:
weight
125
182
160
104
147
KEY CONCEPT
Instantiating an array of objects
reserves room to store references
only. The objects that are stored in
each element must be instantiated
separately.
When we store objects in an array, each element is a separate
object. That is, an array of objects is really an array of object refer-
ences. Consider the following declaration:
String[] words = new String[5];
 
Search WWH ::




Custom Search