<< Chapter < Page Chapter >> Page >

One way to think of this is to think of the second level of array objects as being sub-arrays of the original array object. This construct can be used tocreate multi-dimensional array structures.

(The geometry of such multi-dimensional array structures is not constrained to be rectangles, cubes, etc., as is the requirement in many otherlanguages. Some authors may refer to this as ragged arrays.)

Tree structures

This process of having the components of an array contain references to sub-arrays can be continued indefinitely (well, maybe not indefinitely, but further than I care to contemplate).

(This can be thought of as a tree structure where each array object containing references to other array objects isa node in the tree.)

The leaves of the tree

Eventually, the components (the leaves of the tree structure) must refer to a component type that is not an array type. According to Sun:

"... this is called the element type of the original array, and the components at this level of the data structure are called the elements of the original array."

Component versus element

Hopefully, the above quotation provides a somewhat clearer distinction between the use of the words component and element than was presented earlier but then again, maybe not.

Generic references

Any array object's reference can also be assigned to reference variables of the types Object , Cloneable , or Serializable .

( Object is the class at the top of the inheritance hierarchy. Cloneable and Serializable are interfaces, which are implemented by all array objects. Thus, a reference to an array object can be treated as any ofthese three types.)

Generic array objects

Therefore, if the element type of an array object is one of these types, the elements in the array can refer to:

  • Other array objects
  • Ordinary objects
  • A mixture of the two

This is illustrated in the sample program named Array05 shown in Listing 9 near the end of the module.

Will explain in fragments

I will explain this program in fragments. Listing 4 shows the beginning of the controlling class and the beginning of the main method for the program named Array05 ..

Listing 4 . The beginning of the class named Array05.
public class Array05{ public static void main(String[]args){ int[]v1 = {1,2,3,4,5}; Object[]v2 = new Object[2];

Listing 4 creates two array objects.

An array of type int

The first array object is a five-element array of element type int , with the element values initialized as shown by the values within the curlybrackets. The reference to this array object is assigned to the reference variable named v1 .

An array of element type Object

The second array object is a two-element array of element type Object , with each of the element values initialized to their default value of null . The reference to the array object is assigned to the reference variable named v2 .

(Note that unlike the previous discussion of Object , the declaration of the reference variable in this case does include empty squarebrackets. I will have more to say about this later.)

A new object of this class

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