Java Arrays: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 71: Line 71:


=Array Access=
=Array Access=
An [[#Array_Component|array component]] is accessed by an array access expression that consists of an expression whose value is an array reference followed by an indexing expression enclosed by <code>[</code> and <code>]</code>. Indices are 0-based. An array with length n can be accessed via indexes from 0 to n-1.
<syntaxhighlight lang='java'>
int[] a = {1, 2, 3, 4};
int[][] b = {{11, 22}, {33, 44}};
System.out.println(a[0]);
System.out.println(b[1][1]);
</syntaxhighlight>


=Multidimensional Arrays=
=Multidimensional Arrays=

Revision as of 03:05, 2 November 2021

External

Internal

Overview

Array instances are objects. They are dynamically created, they can be assigned to a variable of type Object and all methods of class Object may be invoked on an array. More details about array creation are available in the Array Creation section below.

An array instance contains a number of variables, called array components. The array components have no names, and they are referenced by array access expressions that use non-negative integer indices. The number of components of an array is fixed, once the array instance is created, the number of components cannot change. If the number of components of an array is zero, the array is said to be empty. If the array has n components, then it is said that the array has the length n. In this case, its components are referenced with zero-based indices from 0 to n-1. More details about accessing array components and elements are available in the Array Access section below.

The components can be of primitive types or reference types. All components of an array have the same type, called the component type of the array. Multidimensional arrays are a special case where all components have array types, recursively.

For unidimensional arrays, the array components are also array elements.

For multidimensional arrays, if starting from the top-level instance, one considers its component type, which is an array type, and then the component type of that type, and so on, eventually one must reach a component type that is NOT an array type. That is called the element type of the original array and the components at that lowest level of the data structure are called the elements of the original array. The element type of an array type may be a primitive type or a reference type (but not an array type). In case the element type is a reference type, the elements of such an array may have as their value a null reference or an instance of the reference type.

int[] a; // a unidimensional array whose components are 
         // ints and whose elements are also ints.
String[] s; // a unidimensional array whose components are 
            // Strings and whose elements are also Strings.
int[][] b; // a bi-dimensional array. b's components are int[], 
           // and the components of those components are ints
float[][][] c; // a tri-dimensional array. c's components are float[][], 
               // their components are float[] and their components are floats.

Array Types

An array type is a kind of a reference type.

If the component type of an array is T, then the type of the array itself is written as the name of its component type followed by an empty pair of square brackets T[]. In case of multidimensional arrays, if the element type of the array is T, then the type of the array is written as the name of the element type followed by a number of empty pairs of square brackets T[]...[]. The number of square brackets pair gives the dimensionality of the array, which is the depth of array nesting.

Each bracket pair in the array type may be annotated by type annotations. The annotation applies to the bracket pair that follows it.

An array length, which can be accessed via the length variable name on the array instance, is not part of the type.

Array Variables

An array variable is a variable of array type that holds a reference to an array instance. Declaring an array variable does not automatically create the array object or allocate space of the array components. It creates only the variable itself, which can contain a reference to an array. To create the array instance use an array creation expression or an initializer.

The array type of the variable depends on the bracket pairs that may appear as part of the type at the beginning of the variable declaration, or as part of the declarator for the variable, or both. Brackets are allowed in declarators as a nod to the tradition of C and C++, but it is not necessarily a recommended style. In the example provided below, a, b and c are all variables that hold references to the same type int[][] arrays:

int[][] a;
int b[][]; // "C-style" declaration, not recommended
int[] c[]; // "mixed notation", not recommended

Because the array length is not part of its type, a single variable of array type may contain references to arrays of different lengths.

If an array variable v has a type A[], where A is a reference type, then v can hold a reference to an instance of an array type B[], provided B can be assigned to A.

Array Creation

Arrays are created by an array creation expression or an array initializer.

Array Creation with an Expression

The array creation expression specifies the element type, the number of levels of nested arrays, and the length of the array for at least the top level of nesting. Lengths for other level of nesting can also be optionally specified, see multidimensional arrays. The array length is available as a final instance variable length.

int[] a = new int[10];
int[][] b = new int [10][]; // dimensions are not fully specified
int[][] c = new int[10][10]; // dimensions are fully specified

Array Creation with an Array Initializer

An array initializer creates an array and provides initial values for all its components:

int[] a = {1, 2, 3, 4};
int[][] b = {{11, 22}, {33, 44}};

Array Access

An array component is accessed by an array access expression that consists of an expression whose value is an array reference followed by an indexing expression enclosed by [ and ]. Indices are 0-based. An array with length n can be accessed via indexes from 0 to n-1.

int[] a = {1, 2, 3, 4};
int[][] b = {{11, 22}, {33, 44}};
System.out.println(a[0]);
System.out.println(b[1][1]);

Multidimensional Arrays

The components of a multidimensional array are themselves of the same array type, and they contain references to subarrays.


Bidimensional Arrays

Tridimensional Arrays