October 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The Essence of OOP using Java, Array Objects, Part 2

  • June 5, 2002
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

First-level access

This array access expression first accesses the contents of the element at index 0 in the array object referred to by the reference variable named v1.  This is a reference to a second array object (note the double matching square brackets, [][]).

Second-level access

The array access expression in Listing 8 uses that reference to access the value stored in the element at index value 1 in the second array object.  That value is then passed to the println method for display on the standard output device.

(In this case, the value 0 is displayed, because array elements are automatically initialized to default values when the array object is created.  The default value for all primitive numeric values is zero.)
Zero-based indexing

All array indexes in Java begin with 0. An array with length n can be indexed by the integers 0 to (n-1).  Array accesses are checked at runtime.  If an attempt is made to access the array with any other index value, an ArrayIndexOutOfBoundsException will be thrown.

Index value types

Arrays must be indexed by integer values of the following types: int, short, byte, or char.  For any of these types other than int, the value will be promoted to an int and used as the index.

An array cannot be accessed using an index of type long.  Attempting to do so results in a compiler error.

Default initialization

If the elements in an array are not purposely initialized when the array is created, the array elements will be automatically initialized with default values.  The default values are:

  • All reference types:  null
  • Primitive numeric types:  0
  • Primitive boolean type:  false
  • Primitive char type:  the Unicode character with 16 zero-valued bits ('\u0000' (the NUL character)
Explicit initialization of array elements

The values in the array elements may be purposely initialized when the array object is created using a comma-separated list of expressions enclosed by matching curly braces.  This is illustrated in Listing 9.
 

    int[] v1 = {1,2,3,4,5};

Listing 9
No new operator
(Note that this format does not make use of the new operator.  Also note that the expressions in the list may be much more complex than the simple literal values shown in Listing 9.)
Length and order

The length of the constructed array will equal the number of expressions in the list.

The expressions in an array initializer are executed from left to right in the order that they occur in the source code.  The first expression specifies the value at index value zero, and the last expression specifies the value at index value n-1 (where n is the length of the array).

Each expression must be assignment-compatible with the array's component type, or a compiler error will occur.

A sample program

The previous paragraphs in this lesson have explained some of the rules and characteristics regarding array objects.  They have also illustrated some of the syntax involved in the use of array objects in Java.

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 array objects are somewhat more powerful than array structures in many other programming languages, and this power often manifests itself in additional complexity.

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 lesson.  This program illustrates three different ways to accomplish essentially the same task using array objects in Java.  That task is to emulate a traditional two-dimensional rectangular array as found in other programming languages.  Two of the ways that are illustrated are essentially ragged arrays with subarrays 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.
 

    Object[][] v1 = new Object[2][3];

Listing 10
Requires all rows to be same length
(Note that unlike the ragged array structures to be discussed later, this approach requires all rows to be the same length.)
Reference variable declaration

The code in Italics (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 holding a reference to an array object, whose elements are capable of holding references to other array objects, whose elements are of type Object.

Two levels of nesting
(The existence of double matching square brackets in the variable declaration in Listing 10 indicates two levels of nesting.)
Not very general

This is not a very general approach.  In particular, 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 whose element 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 array objects must be assignment compatible with the type Object (this includes interface types and class types but not primitive types).



Page 3 of 8



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel