title: How to implement a copy constructor for an array of integers in Java? (school assignment)
body: <p>I want to create a deep copy of this IntArrayList class using a copy constructor. I wrote an empty copy constructor but have no idea what to put in it. I have to complete this for an assignment and show that the results of both the original and copy are independent.</p>
  
  <p>Also, we're not allowed to use library methods like <code>Arrays.copyOf()</code> or <code>System.arrayCopy()</code> etc.</p>
  
  <p>Any help would be greatly appreciated since I have been struggling through this class.</p>
  
  <pre><code>public class IntArrayList {
      /** 
       * Array that holds all integers
       */
         int[] integersArray;
  
      /** 
       * Sets initial size of integersArray
       */
         int initialSize = 4;
  
      /** 
       * Keeps track of the number of elements added and removed from integersArray
       */
         int currentSize = 0;
  
      public IntArrayList(IntArrayList arrayCopy){
  
      }
  
      public static void main(String[] args) {
  
         IntArrayList v = new IntArrayList(4);
  
         //Manually tests the implementation of the methods
         v.add(0);
         v.add(1);
         v.add(2);
         v.add(3);
         v.add(4);
         v.add(5);
         v.add(6);
         v.add(7);
         v.add(8);
         v.add(9);
         v.add(10);
         v.set(0,5);
         v.set(1,1001);
         v.set(2,-1001);
         v.remove(0);
         v.remove(2);
         v.remove(4);
         v.add(27);
         v.remove(2);
         v.toString();
  
      }
  
      /** 
       * Doubles the size of integersArray to accomodate more values 
       */ 
         private void expandArray() {
             int[] temporary;
  
            temporary = new int[integersArray.length*2];
  
                for(int i=0; i&lt;integersArray.length;i++){
                    temporary[i]=integersArray[i];
            }
           int[] temporaryTwo = new int[currentSize+1];
  
            for(int i=0; i&lt;temporaryTwo.length;i++){
                temporaryTwo[i] = temporary[i];
            }
             integersArray = temporaryTwo;
         }
  
      /**
       * Constructs the array of integers and defines its initial size    
       * @param initialSize the integer for the starting size of the internal array
       */
         public IntArrayList(int initialSize){
                 integersArray = new int [initialSize];
  
         }
  
      /**
       * Adds a value to the end of the vector
       * @param val the element that will be added to the internal array
       */
         public void add(int val){
  
  
  
            if(currentSize == integersArray.length){
  
                    expandArray();
  
                integersArray[currentSize] = val;
  
            }else{
           int[] temporary = new int[integersArray.length];
  
           for(int i=0;i&lt;currentSize;i++){
  
                temporary[i]=integersArray[i];           
                   }
           temporary[currentSize] = val;
  
           integersArray = temporary;
            }
            currentSize++;
  
  
         }
  
      /**
       * Removes the last known element in the vector
       */
         public void removeLast(){
  
                 currentSize--;
  
             if(currentSize == integersArray.length){
  
                    int[] temporary = new int[integersArray.length-1];
  
                for(int i=0;i&lt;integersArray.length-1;i++){
                        temporary[i] = integersArray[i];
                }
  
                integersArray = temporary;
  
             }else{
                int[] temporary = new int[integersArray.length-1];
  
            for(int i=0;i&lt;currentSize;i++){
                temporary[i] = integersArray[i];
            }
                    integersArray = temporary;
                System.out.println(temporary);
             }
  
         }
  
      /**
       * Gets the value of any integer within integersArray
       * @param index the value being obtained from the internal array 
       */
         public int get(int index){
                 return integersArray[index];
         }
  
      /**
       * Sets any index in integersArray to any value
       * @param index the location of the element that is being set
       * @param val the element which index is being set to
       */
         public void set(int index, int val) {
                 integersArray[index] = val;
         }
  
      /** 
       * Removes the element at index and shifts all elements after it to the left
       * in the internal array
       * @param index the location of the element that is being removed
       */
         public void remove(int index) throws ArrayIndexOutOfBoundsException{
  
  
                int[] temporary = new int[integersArray.length];
  
            for(int i = 0; i&lt;integersArray.length; i++){
            temporary[i] = integersArray[i];
            }
  
  
            for(int i = index+1; i&lt;currentSize;i++){
                temporary[i-1] = integersArray[i];
            }
            integersArray = temporary;
            currentSize--;
         }
  
      /** 
       * Returns number of elements in the vector
       */
         public int size(){
         int size = currentSize;
  
         return size;
         }
  
      /** 
       * Converts and prints out a string representation of the current
       * state of the vector
       */
         @Override
         public String toString(){
  
  
         int[] temporary = new int[currentSize];
  
         for(int i=0; i&lt;currentSize; i++){
             temporary[i] = integersArray[i];
         }
  
         integersArray = temporary;
  
         String finalArray = Arrays.toString(integersArray);
  
         System.out.print("[");
  
         for(int i=0; i&lt;currentSize; i++){
             System.out.print(integersArray[i] + "," + " ");
                 if(i == currentSize-1){
                 System.out.print(integersArray[i]);
  
                  }
          }
         System.out.print("]");
  
         return finalArray;
         }
  }
  </code></pre>