If, however, the array is of an object type, you must explicitly initialize each element of
the array with a reference to an object:
MyClass ClassArray = new MyClass [10];
for (int i=0; i<10; i++) {
ClassArray[i] = new MyClass; }
Java arrays can have more than one dimension. Multidimensional arrays are created by
using one set of brackets for each dimension, with the same syntax as described above
for a one-dimensional arrays:
int twoDimensionalArray [][] = new int[10][5];
byte fourDimensionalArray [][][][];
fourDimensionalArray = new byte[4][4][5][5];
716 Bonus Day 4
Java arrays start with element 0, and not element 1 as you might think.
Thus, an array with 100 elements has elements [0] through [99], and trying
to use element [100] does not work. If you want an nelement array with
elements [1] through [n]–for example, days[1] through days[365] to refer to
the days of the year—your only choice is to create an array 1 element larger
than needed and ignore element [0].
Caution
Operators ............................................................................................................
Anoperatoris a symbol that performs some operation on data, such as adding and sub-
tracting. You have already met the assignment operator (=), which is used to assign val-
ues. For the most part, operators in Java are identical to those in C and C++. Java has the
standard arithmetic operators for addition (+), subtraction (-), multiplication (*), division
(/), and modulus (%). And, like C and C++, Java offers the unary increment (++) and
decrement (--) operators which, respectively, increase and decrease an integer value by
- Rounding out the mathematical operators are the assignment with operators +=,-=,=,
and/=, which provide a shorthand for operating on the contents of variables. For exam-
ple:
x += 5 ; // Same as x = x + 5;
y = 1.5; // Same as y = y * 1.5;
Java’s comparison operators are also the same as those in C and C++: equal to (==), not
equal to (!=), greater than (>), greater than or equal t0 (>=), less than (<), less than or
equal to (<=).
Finally, Java’s logical operators are also the same as you have learned before: NOT (!),
AND (&&), and OR (||).
39 448201x-Bonus4 8/13/02 11:19 AM Page 716