Brought to you by EarthWeb
ITKnowledge Logo Login Graphic
How smart are you?  Guess the computing class below. ZD University
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Array classes and objects

Arrays are objects. They extend the java.lang.Object class, and you can call toString(), equals(), hashCode(), wait(), notify(), and all the other methods of the object class on a reference variable of array type. An array can be assigned to variables of type Object and passed to methods that expect a reference to an Object type. Arrays live in the heap like all other objects do; therefore, you use reference variables to refer to arrays.

However, there is no java.lang.Array class. Each array has an implicit type of the most primitive type it holds, followed by a number of left-bracket signs ([) equal to its dimension. Thus, an int[][] has type int[[ and a String[] array has type String[. These are not legal Java identifiers, and you won’t find them in Java source code, but you will find them in Java byte code.

As well as the methods inherited from java.lang.Object, arrays have a single field called length. This field contains the length of the array. I used this field above in the line

     for (int i = 0; i < iarray.length; i++) {

Other than this field, arrays mostly inherit the methods of java.lang.Object. The one they override is clone(). Arrays implicitly implement Cloneable, so you can call clone() without getting a CloneNotSupportException. However, the clone of a multidimensional array is a shallow copy. Only the initial references to the sub-arrays are copied, not the sub-arrays themselves.

System.arraycopy()

The System class contains one important method for working with arrays:

     public static void arraycopy(Object src_array, int src_index,
      Object dst_array, int dst_index, int length)

The System.arraycopy() method copies length values from the source array into the destination array. This is used in the StringBuffer and Vector classes as well as many other places. The basic algorithm looks like this:

     for (int i = 0; i < length; i++) {
      dst_array[dst_index+i] = src_array[src_index+i];
     }

In other words, the length components of src_array starting with component src_index are copied in order into the length components of dst_array starting at dst_index. The arrays src_array and dst_array can be the same array. If so, the copy is made as if the source components were first copied into a temporary array and then copied back into the array. This allows overlapping copies to work. If the arrays have reference types, then the copy is a shallow copy. That is, only the references are copied. The objects to which the references point are not copied.

If the arrays to be copied contain primitive data types, they must contain the same primitive data type. For example, an array of shorts can only be copied to another array of shorts, not to an array of ints or longs. This is one of the few places in Java where arithmetic promotion does not take place. If you try to copy an array of one primitive type such as short to an array of a wider primitive type such as int, then an ArrayStoreException is thrown and the destination array is left unchanged. Similarly if you try to copy an array of a reference type to an array of primitive type or vice versa, an ArrayStore-Exception is thrown, and the destination array is left unchanged.

Copies between arrays of reference types are a little more complex. As long as the reference type in one array can be converted to the reference type of the other array, the copy takes place. Thus, it is acceptable to copy a Float[] array to a Number[] array or an Object[] array to a String[] array because Floats can be cast to Numbers and Objects can be cast to Strings. (The latter cast works only if the Objects are in fact Strings. Otherwise, an ArrayStoreException will be thrown.) However if the reference types are incompatible — for example, Float and String — then an ArrayStoreException will be thrown.

It’s possible for some of the components of the source array to be compatible with the type of the destination array and some to be incompatible. For example, if src_array has type Object[], then it can contain both Floats and Strings. If dst_array has type String, then you can copy the Strings from src_array to dst_array but not the Floats. In this case, the components of the src_array that can be copied starting with component 0 are copied. However, as soon as an incompatible component is encountered, an ArrayStoreException is thrown, and no further components are copied, compatible or incompatible.

This method can also throw an ArrayIndexOutOfBoundsException, in which case the destination array is not changed. (The source array is never changed.) An ArrayIndexOutOfBoundsException is thrown if srcIndex, dstIndex, or length is negative, if srcIndex+length is greater than srcArray.length, or if dstIndex+length is greater than dst.length.

The System.arraycopy() method is normally implemented in native code for maximum performance. While it would be possible to write an equivalent routine in pure Java, most architectures have extremely efficient native instructions for copying large blocks of memory from one place to another. Because an array is natively a large block of memory, this is one of the places where native code helps a lot.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.