Here is an example of an array declaration and instantiation for the array of the following string values:
``red''
``blue''
``white''
The code:
String primary []
=
new String [3];
primary [0]
=
``red'';
primary [1]
=
``blue'';
primary [2]
=
``white'';
As you can see, primary [0] is bound to ``red'' and so on. It is important to note that in the instantiation, String [3] denotes the length of the string array. The first ``whatever'' in an array is always denoted [0].
Here is some more array code:
Suppose ' a' is an integer array, then:
1)
read a new value into the first element of array a.
a [0]
=
IO.read_int ();
2)
add 10 to the first element of array a.
a [0]
=
a [0]
+
10;
3)
display the values of array a, one per line and using only one IO statment.
for (int
x
=
0;
x
< =
4;
x
=
x
+
1)
{
IO.println (a [x]);
}
|