An array provides an ordered, sequential set of elements. These
elements consist of either primitive values or references to objects.
Here we will concentrate on the primitive type arrays.
We will discuss arrays in more detail in the next
chapter. However, they are very useful for the demonstration
programs and exercises, so we will give a brief introduction to
implementing arrays of primitive type values.
You declare a reference for a one dimensional array of primitive
type values in two optional ways:
int
iArray[];
or
float
[] fArray;
That is, you can put the brackets in front of or after the array
name in the declaration.
In Java, arrays are actually objects that contain the data sequence.
So just as for any class, you create an an instance of an array
with the new
operator. For example:
int
[] iArray = new int[10]; // int array with 10 elements
long lArray[] = new long[20];// long array
with 20 elements
int N = 15;
short sArray[] = new short[N];// short array
with 15 elements
For arrays of primitive type values, the new
operation creates both the array object and the space in memory
for each primitive element.
(Don't confuse the internal array class with the java.util.Arrays
class that provides various tools for sorting, filling and searching
an array.)
Each element of a primitive type array will hold a default value
For numeric types, the default values equal zero (0 for integers,
0.0 for floating point). For boolean
arrays the values equal false. For char
the default is '\u0000'.
Note: The size of the array
in the declaration must be an int
integer. So arrays are limited to the maximum
value of a int,
which is 2147483647.
You can also create and initialize an array to particular values
in the declaration by putting the elements between parentheses and
separated by commas, as in
int
[] iArray = { 1, 3 , 5, 6}; // int array with 4 elements
long lArray[] = {10, 5, 3}; //
long array with 3 elements
double dArray[] = {1.0, 343.33, -13.1}; //double
array with 3 elements
You access the elements by using an int
type as an index with a value between 0
and N-1,
where N
equals the size of the array. For example,
int ii = iArray[3] * 5;
double d = dArray [0] / 3.2;
You can find the size of an array via the length
property.
int arraySize
= iArray.length;//length is a data field in the array class
Note again that the above declarations
create arrays of primitive type values. For arrays of objects, an
object must be created separately for each element in the array.
In other words, the array is really just a list of references to
other objects. The object for each element must be created in a
separate step. We will discuss arrays of objects in the next
chapter.
Below we give a simple demonstration program showing the basics
of creating, initializing, and using primitive type arrays.
ArrayApplet.java
(Output goes to browser's Java
console.) |
public
class ArrayApplet extends java.applet.Applet
{
public void init () {
// Declare an int array
int [] ia = new int[4];
// Use a for loop to fill it.
for (int i=0; i < 4; i++) {
ia[i] =
i*2;
}
// Create a double type array with
four values
double [] fa = {1.4, 3.3, 2.4, 9.4};
// Use a four loop to print out
paired values for
// the two arrays.
for (int i=0; i < 4; i++) {
System.out.println
(i + ". (" + ia[i] + ", " + fa[i] + ")");
}
}
// Paint message in Applet window.
public void paint (java.awt.Graphics g) {
g.drawString ("ArrayApplet", 20,
20);
}
}
|
|
Latest update: Oct. 19, 2004
|