Arrays are a common data type utilized in the Java language, so it is worthwhile to mention
some of the details involved in copying arrays. In the next example you will see the difference
between copying an array's elements to another array called deep copying, and copying the
pointers from one array to another array.
Here is the example:
In the above example, scroll down to "itemStateChanged" that is where the array copying occurs.
If cbDeep is selected then a new object is constructed for each element of the array which
is called deep copying. If cbShallow is selected then the static function "arraycopy" of System
is called and each of the pointers to the individual elements are copied which is called
When using the "clone" function on Objects of the java.util package shallow copies are made
unless otherwise noted. I am including two examples of how you can create deep copies
using a static method call. This form of deep copying only works if the elements of
the Vector or Hashtable are implementors of java.io.Serializable .
Here is the example of deep copying a Vector.
Here is the example of deep copying a Hashtable.
In this lesson, you learned the difference between deep copying and shallow copying
and how to implement arrays with either of the two. You also were provided with
functions for deep copying Vectors and Hashtables which utilize object serialization.