<< Chapter < Page Chapter >> Page >

The reference variable named v1 refers to the array object that forms the root of the tree.

Each of the two elements in the root array object is initialized with a reference to another array object.

(These two objects might be viewed as sub-arrays, or as child nodes in the tree structure).

Each of the child nodes is an array object of type Object ,, and has a length of three.

Each element in each of the two child node array objects is initialized to the value null (this is the default initialization for array elements of reference types that don't yet refer to an object).

Recap

To recap, the reference variable named v1 contains a reference to a two-element, one-dimensional array object. Each element in that array object iscapable of storing a reference of type Object[] (a reference to another one-dimensional array object of type Object ).

Two sub-array objects

Two such one-dimensional sub-array (or child node) objects, of element type Object , are created. References to the two sub-array objects are stored in the elements of the two-element array object at the root of the tree.

Each of the sub-array objects has three elements. Each element is capable of storing a reference to an object as type Object .

The leaves of the tree

These two sub-array objects might be viewed as the leaves of the tree structure.

Initialize elements to null

However, the objects of type Object don't exist yet. Therefore, each element in each of the sub-array objects is automatically initialized to null .

Arrays versus sub-arrays

Note that there is no essential difference between an array object and a sub-array object in the abovediscussion. The use of the sub prefix is used to indicate that an ordinary array object belongs to another array object, because the reference to the sub-arrayobject is stored in an element of the owner object.

Many dimensions are possible

Multi-dimensional arrays of any (reasonable) depth can be emulated in this manner. An array object may contain references to other array objects, which may contain references to other array objects, and so on.

The leaves of the tree structure

Eventually, however, the elements of the leaves in the tree structure must be specified to contain either primitive values or references to ordinary objects. This is where thedata is actually stored.

(Note however, that if the leaves are specified to contain references of type Object , they may contain references to other array objects of any type, and the actual data could be stored in those array objects.)

The length of an array

Every array object contains a public final instance variable named length , which contains an integer value specifying the number of elements in the array.

Once created, the length of the array encapsulated in an array object cannot change. Therefore, the value of length specifies the length of the array throughout the lifetime of the array object.

Using length to populate the leaves of the tree structure

The value of length is very handy when processing array objects. This is illustrated in Listing 11 , which uses a nested for loop to populate the elements in the leaves of the tree structure referred to by v1 . (The elements in the leaf objects are populated with references to objects of type Integer . Note that Integer is not a primitive type. Instead, it is a wrapper class for primitive data of type int .)

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask