Using Arrays
An Introduction with Examples in Java |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
An array is a contiguous block of memory that can be used to hold multiple homogeneous elements.
[]
Modifier:
int[] income;
[]
Operator:
income[5] = 250000;
total = income[3];
One can declare an array and assign all of the elements to it using an "array literal" (formally, an array initializer) as follows:
One can then assign elements or access elements using the []
operator.
One can declare an array and assign default values to all of the elements to it as follows:
One can then assign elements or access elements using the []
operator.
ArrayIndexOutOfBounds
"error" will be
generated)