/**
 * Arrays:
 * 
 * 1. All elements of an array will have the same type,
 *    say T.
 * 
 * 2.  All elements of an array behave exactly like an
 *   ordinary variable of the array's type T.
 * 
 * 3.  An array is an object.
 * 
 * 4.  The number of elements in an array cannot change
 *   once we've initialized it.
 * 
 * 5.  The type of the array T[] is read as "array of T".
 */ 
public class ArrayDemo {

  /**
   * Display the contents of a on the screen from index 
   * i to index j.  Assume that i <= j.
   * 
   *@param a  the input array
   *@param i  the start index (inclusive)
   *@param j the end index (inclusive)
   */ 
  public static void displayArray(int[] a, int i, int j) {
    for (int k = i; k <= j; k++) {
      System.out.println(a[k]);
    }
  }
  
  /**
   * Return the index of value e in the array a, or -1
   * if that value is not in the array.
   * 
   *@param a the input array
   *@param e the value to search for
   *@return the index of e in array a. -1 if e is not in the array
   */
  public static int findElement(int[] a, int e) {
    int index = -1;
    
    for (int i = 0; i < a.length; i++) {
      if (a[i] == e) {
        index = i;
      }
    }
    return index;
  }
  
  
  /**
   * Return the index of value e in the array a, or -1
   * if that value is not in the array.
   * 
   *@param a the input array
   *@param e the value to search for
   *@return the index of e in array a. -1 if e is not in the array
   */
  public static int findElementV2(int[] a, int e) {
    int index = -1;
    boolean found = false;
    
    for (int i = 0; i < a.length && (!found); i++) {
      if (a[i] == e) {
        index = i;
        found = true;
      }
    }
    return index;
  }

  /**
   * Return the index of value e in the array a, or -1
   * if that value is not in the array.
   *@param a the input array
   *@param the value to search for
   *@return the index of e in array a. -1 if e is not in the array
   */
  public static int findElementV3(int[] a, int e) {
    // exercise: instead of a for loop, use a while loop to write this method
    return 0;
  }
  
  /**
   * Copy the contents of array a into a new array
   * of length l, and return the new array.  
   * Precondition: l >= a.length
   */
  public static int[] copyArray(int[] a, int l) {
    int[] newArray = new int[l];
    for (int i = 0; i < a.length; i++) {
      newArray[i] = a[i];
    }
    return newArray;
  }
  
  /**
   * check whether the contents of two arrays are the same
   * @param a1 first array
   * @param a2 second array
   * @return true if and only if either of the following are true:
   *         1) a1 and a2 are both null
   *         2) a1 and a2 have the same length and elements
   */
  public static boolean equals(int[] a1, int[] a2){
    boolean result = false;
    
    if (a1 == a2) { // also checks if a1 and a2 are both null
      result = true;
    } else if (a1 == null || a2 == null) { // checks if only one of arrays is null
      result = false;
    } else if (a1.length == a2.length) {
      result = true;
      for (int i = 0; i < a1.length && result; i++) {
        if (a1[i] != a2[i]) {
          result = false;
        }
      }
    } else {
      result = false;
    }
    
    return result;
  }

  /**
   * check whether the contents of two arrays are the same.
   * This is the second version. not much different from above.
   * @param a first array
   * @param b second array
   * @return true if and only if either of the following are true:
   *         1) a and b are both null
   *         2) a and b have the same length and elements
   */
  public static boolean equalsV2(int[] a, int[] b){
    // exercise: write this method without using any for loops
    return false;
  }  
  
   /**
   * Swap element i with element j (in place, i.e. it does
   * not create a new array).
   * @param a the input array
   * @param i index of the first element to be swapped
   * @param j index of the second element to be swapped
   * @return the updated array with elments at index i and j swapped
   */
  public static int[] swap(int[] a, int i, int j) {   
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    return a;
  }

}

