<< Chapter < Page Chapter >> Page >
Listing 5 . Initialization.
int[] v1 = new int[3];

Can refer to different array objects

The length of an array is not established when the reference variable is declared. As with references to ordinary objects, a reference to anarray object can refer to different array objects at different points in the execution of a program.

For example, a reference variable that is capable of referring to an array of type int[] can refer to an array object of a given length at one point in the program and can refer to a different array object of thesame type but a different length later in the program.

Placement of square brackets

When declaring an array reference variable, the square brackets [] may appearas part of the type, or following the variable name, or both. This is illustrated in Listing 6 .

Listing 6 . Placement of square brackets.
int[][]v1; int[]v2[];int v3[][];

Type and length

Once an array object is created, its type and length never changes. A reference to a different array object must be assigned to the reference variableto cause the reference variable to refer to an array of different length.

Creating the actual array object

An array object is created by an array creation expression or an array initializer.

An array creation expression (or an array initializer) specifies:

  • The element type
  • The number of levels of nested arrays
  • The length of the array for at least one of the levels of nesting

Two valid array creation expressions are illustrated by the statements in Listing 7 .

Listing 7 . Creating the actual array object.
int[][]v1; int[]v2[];v1 = new int[2][3]; v2 = new int[10][];

A two-dimensional rectangular array

The third statement in Listing 7 creates an array object of element type int with two levels of nesting. This array object can be thought of as a traditional two-dimensional rectangular array having two rows and three columns. (This is a somewhat arbitrary choice as to which dimension specifies the number of rows and which dimension specifies the number of columns. You mayprefer to reverse the two.)

A ragged array

The fourth statement also creates an array object of element type int with two levels of nesting. However, the number of elements in each column is not specified at this point, and it is not appropriate to think ofthis as a two-dimensional rectangular array. In fact, once the number of elements in each column has been specified, it may not describe a rectangle atall. Some authors refer to an array of this type as a ragged array.

The length of the array

The length of the array is always available as a final instance variable named length . I will show you how to use the value of length in a sample program later in this module.

Accessing array elements

An array element is accessed by an array access expression . The access expression consists of an expression whose value is an array reference followedby an indexing expression enclosed by matching square brackets.

The expression in parentheses in Listing 8 illustrates an array access expression (or perhaps two concatenated array access expressions).

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