📄 10.doc.html
字号:
<html>
<head>
<title>The Java Language Specification Arrays</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="9.doc.html">Prev</a> | <a href="11.doc.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="27803"></a>
<p><strong>
CHAPTER 10 </strong></p>
<a name="27805"></a>
<h1>Arrays</h1>
<hr><p>
<a name="25550"></a>
Java <i>arrays</i> are objects <a href="4.doc.html#12028">(§4.3.1)</a>, are dynamically created, and may be assigned
to variables of type <code>Object</code> <a href="4.doc.html#11055">(§4.3.2)</a>. All methods of class <code>Object</code> may be invoked
on an array.
<p><a name="25500"></a>
An array object contains a number of variables. The number of variables may be zero, in which case the array is said to be <i>empty</i>. The variables contained in an array have no names; instead they are referenced by array access expressions that use nonnegative integer index values. These variables are called the <i>components</i> of the array. If an array has <i>n</i> components, we say <i>n</i> is the <i>length</i> of the array; the components of the array are referenced using integer indices from 0 to <img src="10.doc.anc1.gif">, inclusive.<p>
<a name="25501"></a>
All the components of an array have the same type, called the <i>component type</i> of the array. If the component type of an array is <i>T</i>, then the type of the array itself is written <i>T</i><code>[]</code>.<p>
<a name="25502"></a>
The component type of an array may itself be an array type. The components of such an array may contain references to subarrays. If, starting from any array type, one considers its component type, and then (if that is also an array type) the component type of that type, and so on, eventually one must reach a component type that is not an array type; this is called the <i>element type</i> of the original array, and the components at this level of the data structure are called the <i>elements</i> of the original array.<p>
<a name="25514"></a>
There is one situation in which an element of an array can be an array: if the element type is <code>Object</code>, then some or all of the elements may be arrays, because any array object can be assigned to any variable of type <code>Object</code>.<p>
<a name="25518"></a>
<h2>10.1 Array Types</h2>
<a name="25519"></a>
An array type is written as the name of an element type followed by some number
of empty pairs of square brackets <code>[]</code>. The number of bracket pairs indicates the
depth of array nesting. An array's length is not part of its type.
<p><a name="30943"></a>
The element type of an array may be any type, whether primitive or reference. In particular:<p>
<ul><a name="26084"></a>
<li>Arrays with an interface type as the component type are allowed. The elements of such an array may have as their value a null reference or instances of any class type that implements the interface.
<a name="26075"></a>
<li>Arrays with an <code>abstract</code> class type as the component type are allowed. The elements of such an array may have as their value a null reference or instances of any subclass of the <code>abstract</code> class that is not itself <code>abstract</code>.
</ul><a name="25523"></a>
Array types are used in declarations and in cast expressions <a href="15.doc.html#238146">(§15.15)</a>.<p>
<a name="25891"></a>
<h2>10.2 Array Variables</h2>
<a name="17235"></a>
A variable of array type holds a reference to an object. Declaring a variable of
array type does not create an array object or allocate any space for array components.
It creates only the variable itself, which can contain a reference to an array.
However, the initializer part of a declarator <a href="8.doc.html#40898">(§8.3)</a> may create an array, a reference
to which then becomes the initial value of the variable.
<p><a name="17236"></a>
Because an array's length is not part of its type, a single variable of array type may contain references to arrays of different lengths.<p>
<a name="25894"></a>
Here are examples of declarations of array variables that do not create arrays:<p>
<pre><a name="25895"></a>
int[] ai; // array of int
<a name="25896"></a>short[][] as; // array of array of short
<a name="25897"></a>Object[] ao, // array of Object
<a name="25898"></a> otherAo; // array of Object
<a name="25899"></a>short s, // scalar short
<a name="25901"></a> aas[][]; // array of array of short
</pre><a name="25902"></a>
Here are some examples of declarations of array variables that create array
objects:
<p><pre><a name="25903"></a>
Exception ae[] = new Exception[3];
<a name="25904"></a>Object aao[][] = new Exception[2][3];
<a name="25905"></a>int[] factorial = { 1, 1, 2, 6, 24, 120, 720, 5040 };
<a name="25906"></a>char ac[] = { 'n', 'o', 't', ' ', 'a', ' ',
<a name="25907"></a> '<code>S</code>', 't', 'r', 'i', 'n', 'g' };
<a name="25908"></a>String[] aas = { "array", "of", "String", };
</pre><a name="25909"></a>
The <code>[]</code> may appear as part of the type at the beginning of the declaration, or as
part of the declarator for a particular variable, or both, as in this example:
<p><pre><a name="25910"></a>byte[] rowvector, colvector, matrix[];
</pre><a name="25911"></a>
This declaration is equivalent to:
<p><pre><a name="25929"></a>byte rowvector[], colvector[], matrix[][];
</pre><a name="25915"></a>
Once an array object is created, its length never changes. To make an array variable
refer to an array of different length, a reference to a different array must be
assigned to the variable.
<p><a name="25953"></a>
If an array variable <i>v</i><i></i> has type <i>A</i><i></i><code>[]</code>, where <i>A</i><i></i> is a reference type, then <i>v</i><i></i> can hold a reference to an instance of any array type <i>B</i><code>[]</code>, provided <i>B</i><i></i> can be assigned to <i>A</i><i></i>. This may result in a run-time exception on a later assignment; see <a href="10.doc.html#11430">§10.10</a> for a discussion.<p>
<a name="25959"></a>
<h2>10.3 Array Creation</h2>
<a name="25650"></a>
An array is created by an array creation expression <a href="15.doc.html#46168">(§15.9)</a> or an array initializer
<a href="10.doc.html#11358">(§10.6)</a>.
<p><a name="25675"></a>
An array creation expression specifies the element type, the number of levels of nested arrays, and the length of the array for at least one of the levels of nesting. The array's length is available as a final instance variable <code>length</code>.<p>
<a name="25676"></a>
An array initializer creates an array and provides initial values for all its components. (Contrast this with C and C++, where it is possible for an array initializer to specify initial values for some but not all of the components of an array.)<p>
<a name="25566"></a>
<h2>10.4 Array Access</h2>
<a name="53523"></a>
A component of an array is accessed by an array access expression <a href="15.doc.html#239587">(§15.12)</a> that
consists of an expression whose value is an array reference followed by an indexing
expression enclosed by <code>[</code> and <code>]</code>, as in <code>A[i]</code>. All arrays are <code>0</code>-origin. An array
with length <i>n</i> can be indexed by the integers <code>0</code> to <i>n</i><code>-1</code>.
<p><a name="25570"></a>
Arrays must be indexed by <code>int</code> values; <code>short</code>, <code>byte</code>, or <code>char</code> values may also be used as index values because they are subjected to unary numeric promotion <a href="5.doc.html#170952">(§5.6.1)</a> and become <code>int</code> values. An attempt to access an array component with a <code>long</code> index value results in a compile-time error.<p>
<a name="53999"></a>
All array accesses are checked at run time; an attempt to use an index that is less than zero or greater than or equal to the length of the array causes an <code>IndexOutOfBoundsException</code>  to be thrown.<p>
<a name="54000"></a>
<h2>10.5 Arrays: A Simple Example</h2>
<a name="25421"></a>
The example:
<p><pre><a name="25422"></a>
class Gauss {
<a name="25426"></a> public static void main(String[] args) {
<a name="25427"></a> int[] ia = new int[101];
<a name="25428"></a> for (int i = 0; i < ia.length; i++)
<a name="25429"></a> ia[i] = i;
<a name="25435"></a> int sum = 0;
<a name="25430"></a> for (int i = 0; i < ia.length; i++)
<a name="25436"></a> sum += ia[i];
<a name="25443"></a> System.out.println(sum);
<a name="25444"></a> }
<a name="25453"></a>}
</pre><a name="25454"></a>
that produces output:
<p><pre><a name="25462"></a>5050
</pre><a name="25463"></a>
declares a variable <code>ia</code> that has type array of <code>int</code>, that is, <code>int[]</code>. The variable <code>ia</code> is
initialized to reference a newly created array object, created by an array creation
expression <a href="15.doc.html#46168">(§15.9)</a>. The array creation expression specifies that the array should
have <code>101</code> components. The length of the array is available using the field <code>length</code>,
as shown.
<p><a name="25585"></a>
The example program fills the array with the integers from <code>0</code> to <code>100</code>, sums these integers, and prints the result.<p>
<a name="11358"></a>
<h2>10.6 Arrays Initializers</h2>
<a name="25749"></a>
An <i>array initializer</i> may be specified in a declaration, creating an array and providing
some initial values:
<p><ul><pre>
<i>ArrayInitializer:<br>
</i> <code>{ </code><i>VariableInitializers</i><sub><i>opt</i></sub><code> ,</code><sub><i>opt</i></sub><code> }
</code>
<i>VariableInitializers:<br>
</i> <i>VariableInitializer<br>
</i> <i>VariableInitializers</i><code> , </code><i>VariableInitializer
</i></pre></ul><a name="25741"></a>
The following is repeated from <a href="8.doc.html#40898">§8.3</a> to make the presentation here clearer:
<p><ul><pre>
<i>VariableInitializer:<br>
</i> <i>Expression<br>
</i> <i>ArrayInitializer
</i></pre></ul><a name="25727"></a>
An array initializer is written as a comma-separated list of expressions, enclosed by braces "<code>{</code>" and "<code>}</code>".<p>
<a name="25754"></a>
The length of the constructed array will equal the number of expressions.<p>
<a name="25755"></a>
Each expression specifies a value for one array component. Each expression must be assignment-compatible <a href="5.doc.html#170768">(§5.2)</a> with the array's component type, or a compile-time error results.<p>
<a name="25756"></a>
If the component type is itself an array type, then the expression specifying a component may itself be an array initializer; that is, array initializers may be nested.<p>
<a name="11390"></a>
A trailing comma may appear after the last expression in an array initializer and is ignored.<p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -