Java Reference
In-Depth Information
Array “lastName”
Array “dependents”
index
Whiteman
Jones
Lopez
Carlson
Ragavendra
Black
Smith
Kruger
Thompson
Stone
0
2
3
1
2
5
4
2
1
1
0
1
2
3
4
5
6
7
8
9
Figure6-1RepresentationofArrays
An arrayelement isanindividualdatavalue.Eachelementinanarray
canbeconsideredasastand-alonevariable.Inthearrayof Figure6-1 ,
Carlson is the fourth element of the array “lastName”. Because the array
index is zero-based,the fourth entry is located at index 3. The number of
dependents for this employee is found at the same index,in the array
named “dependents”. In this case Carlson has one dependent. Note that
to identify an array element you need to use the array name and the corre-
sponding index.
Creating an array
In Java there are three distinct operations with arrays:
1. The array declaration
2. The array creation or allocation
3. The array initialization or access
An array of type int is declared as follows:
int[] studentGrades;
The same array is actually created using the new operator
finalGrade = new int[14];
The declaration and the creation of an array can also be performed in a
single statement:
int[] finalGrade = new int[14];
 
Search WWH ::




Custom Search