ARRAYS
Array Access
10.4
The
[]
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:
byte[] rowvector, colvector, matrix[];
This declaration is equivalent to:
byte rowvector[], colvector[], matrix[][];
Once an array object is created, its length never changes. To make an array vari
able refer to an array of different length, a reference to a different array must be
assigned to the variable.
If an array variable
v
has type
A[]
, where
A
is a reference type, then
v
can
hold a reference to an instance of any array type
B[]
, provided
B
can be assigned
to
A
. This may result in a run time exception on a later assignment; see 10.10 for
a discussion.
10.3 Array Creation
An array is created by an array creation expression ( 15.9) or an array initializer
( 10.6).
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
length
.
An array initializer creates an array and provides initial values for all its com
ponents. (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.)
10.4 Array Access
A component of an array is accessed by an array access expression ( 15.12) that
consists of an expression whose value is an array reference followed by an index
ing expression enclosed by
[
and
]
, as in
A[i]
. All arrays are
0
origin. An array
with length
n
can be indexed by the integers
0
to
n 1
.
Arrays must be indexed by
int
values;
short
,
byte
, or
char
values may also
be used as index values because they are subjected to unary numeric promotion
( 5.6.1) and become
int
values. An attempt to access an array component with a
long
index value results in a compile time error.
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
IndexOutOfBoundsException
to be thrown.
195
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved