<< Chapter < Page | Chapter >> Page > |
More powerful and complex
Many aspiring Java programmers find the use of array objects to be something less than straightforward, and that is understandable. In fact, Java arrayobjects are somewhat more powerful than array structures in many other programming languages, and this power often manifests itself in additionalcomplexity.
A traditional two-dimensional rectangular array
Some of that complexity is illustrated by the program named Array07 , shown in Listing 26 near the end of this module. This program illustrates three different ways to accomplish essentially the same taskusing array objects in Java. That task is to emulate a traditional two-dimensional rectangular array as found in other programming languages. Twoof the ways that are illustrated are essentially ragged arrays with sub-arrays having equal length.
Ragged arrays
The program also illustrates two different ways to work with array objects and ragged arrays.
Will discuss in fragments
As is my practice, I will discuss and explain the program in fragments.
All of the interesting code in this program is contained in the main method, so I will begin my discussion with the first statement in the main method.
Create a two-dimensional rectangular array structure
Listing 10 creates an array structure that emulates a traditional rectangular array with two rows and three columns.
Listing 10 . Create a two-dimensional rectangular array structure. |
---|
Object[][]v1 = new Object[2][3]; |
(Note that unlike the ragged array structures to be discussed later, this approach requires all rows to be the same length and all columns to bethe same length.)
Reference variable declaration
The code to the left of the equal sign (=) in Listing 10 declares a reference variable named v1 . This reference variable is capable of holding a reference to an array object whose elements are of the type Object[] ..
In other words, this reference variable is capable of
Two levels of nesting
The existence of double matching square brackets in the variable declaration in Listing 10 indicates two levels of nesting.
Restrictions
The elements in the array object referred to by v1 can only hold references to other array objects whose element type is Object (or references to array objects whose element type is a subclass of Object ).
The elements in the array object referred to by v1 cannot hold references to ordinary objects instantiated from classes, or array objects whoseelement type is a primitive type.
In other words, the elements in the array object referred to by v1 can only hold references to other array objects. The element types of those arrayobjects must be assignment compatible with the type Object (this includes interface types and class types but not primitive types).
A tree of empty array objects
The code to the right of the equal sign (=) in Listing 10 creates a tree structure of array objects. The object at the root of the tree is an array object of type Object[] , having two elements (a length of two).
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?