Java Reference
In-Depth Information
Although I did introduce arrays as a type of object, Java does not treat arrays
simply as standard objects. There is enough special syntax in the language for arrays
to view them as a special type of reference variable, slightly different from objects.
For example, the assignment and evaluation statements for arrays that contain
primitive data types are very similar to the same statements used by the primitive
data type. This syntax is valid for arrays: if (errorNumbersIO[x] == 30) . It is quite
similar to the syntax used by primitive integer types: if (errorNumber == 30) . If ar-
rays were simply objects, this syntax would compare the objects in the arrays, not
the numeric values.
Java also provides a set of useful array-specific methods. In order to copy
some part of an array into another array, use the arraycopy() function in the Java
System class:
System.arraycopy (sourceArray, int sourcePosition,
destinationArray, int destinationPosition, int
numberOfEntriesToCopy);
Suppose you want to copy the I/O error numbers array and the logical error
numbers array into a single array. You could use the arraycopy() method to do this:
int[] errorNumbersIO = {1, 2, 10, 22, 23};
int[] errorNumbersLogical = {101, 102, 108, 122};
int[] errorNumbersAll = new int[errorNumbersIO.length
+ errorNumbersLogical.length];
System.arraycopy (errorNumbersIO, 0,
errorNumbersAll, 0, 5);
System.arraycopy (errorNumbersLogical, 0,
errorNumbersAll, 5, 4);
// A temporary array can be defined, and then it can be assigned to any of
// these arrays.
int[] tempNumbers;
tempNumbers = errorNumbersIO;
tempNumbers = errorNumbersLogical;
tempNumbers = errorNumbersAll;
Arrays can be passed into methods and returned from a method as its return
value. This is very useful when a method needs to return a set of values instead
of just one.
Search WWH ::




Custom Search