[ Pobierz całość w formacie PDF ]
}
Array cloning overrides the protected Object method that would normally throw a
CloneNotSupportedException with a public one that actually works.
Array Immutability
It is useful to return an array clone from a method if you don't want the caller of your method to modify the
underlying array structure. While you can declare arrays to be final, as in the following example:
final static int array[] = {1, 2, 3, 4, 5};
declaring an object reference final (specifically, an array reference here) does not restrict you from modifying
the object. It only limits you from changing what the final variable refers to. While the following line results
in a compilation error:
array = new int[] {6, 7, 8, 9};
changing an individual element is perfectly legal:
array[3] = 6;
Tip Another way to "return" an immutable array from a method is to return an Enumeration or
Iterator into the array, rather than returning the actual array. Either interface provides access to
the individual elements without exposing the whole array to changes or requiring you to make a
copy of the entire array. You'll learn more about these interfaces in later chapters.
Array Assignments
Array assignments work like variable assignments. If variable x is a reference to an array of y, then x can be a
reference to z if a variable of type z can be assigned to y. For instance, imagine that y is the AWT Component
class and z is the AWT Button class. Because a Button variable can be assigned to a Component variable, a
14
Checking for Array Equality
Button array can be assigned to a Component array:
Button buttons[] = {
new Button ("One"),
new Button("Two"),
new Button("Three")};
Component components[] = buttons;
When an assignment like this is made, both variables' buttons and components refer to the same heap space in
memory, as shown by Figure 2-6. Changing an array element for one array changes the element for both.
Figure 2-6: Shared memory after an array assignment.
If, after assigning an array variable to a superclass array variable (as in the prior example of assigning the
button array to a component array variable) you then try to place a different subclass instance into the array,
an ArrayStoreException is thrown. To continue the prior example, an ArrayStoreException would be thrown
if you tried to place a Canvas into the components array. Even though the components array is declared as an
array of Component objects, because the components array specifically refers to an array of Button objects,
Canvas objects cannot be stored in the array. This is a run-time exception as the actual assignment is legal
from the perspective of a type-safe compiler.
Checking for Array Equality
Checking for equality between two arrays can be done in one of two manners depending upon the type of
equality you are looking for. Are the array variables pointing to the same place in memory and thus pointing
to the same array? Or are the elements of two arrays comparatively equivalent?
Checking for two references to the same memory space is done with the double equal sign operator ==. For
example, the prior components and buttons variables would be equal in this case since one is a reference to the
other:
components == buttons // true
However, if you compare an array to a cloned version of that array then these would not be equal as far as ==
goes. Since these arrays have the same elements but exist in different memory space, they are different. In
order to have a clone of an array be "equal" to the original, you must use the equals() method of the
java.util.Arrays class.
String[] clone = (String[]) strarray.clone();
boolean b1 = Arrays.equals(strarray, clone); // Yes, they're equal
15
Array Reflection
This will check for equality with each element. In the case where the arguments are arrays of objects, the
equals() method of each object will be used to check for equality. Arrays.equals() works for arrays that are not
clones, too. For more information on the Arrays class, see Chapter 13.
Array Reflection
If for some reason you are ever unsure whether an argument or object is an array, you can retrieve the object's
[ Pobierz całość w formacie PDF ]