20-CS-694 Advanced Programming Techniques Spring 2012
Arrays

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

All lectures
Creation, Parameter Passing, Cloning

Instructions for all applets:

All applets contain a 'Press Me' button. Click it.

    1.   Array1Test.java

    Demonstration of array creation. The declaration
      A[] a;
is merely a pointer to an array of type A objects. The following
      A[] a = new A[10];
creates an array of pointers to A objects. The following
      a[5] = new A(34);
actually adds an element to array a.
 
    2.   Array2Test.java

    Example of the declaration and initialization of a two dimensional array. Notice that the number of columns can vary from row to row. The format of initialization, for this example, is:
      A[][] a = { { ... }, { ... } };
 
    3.   Array3Test.java

    The class System has a static method called arraycopy which makes a deep copy of a one dimensional array. To make a deep copy of a multi-dimensional, do arraycopy on lowest level rows like this example:
   A[][] c = { { ... }, { ... } };
   A[][] d = new A[2][];
   d[0] = new A[3];
   d[1] = new A[4];
   for (int i=0 ; i < 2 ; i++)
      System.arraycopy(c[i],0,d[i],0,c[i].length);
 
    4.   Array4Test.java

    This example shows how to pass arrays as arguments of functions. Three functions are written: one does a shallow copy of the input array, another does a deep copy using the overridden clone method, and one prints the array.
 
    5.   Array5Test.java

    Same as above but shows that cloning of an array is not deep.