public class ArrayDemo {
 
  /** 
   * Take each character of a String and 
   * store it in an array of characters.
   */
  public static char[] storeChars(String s) {
    char[] c = null;
    
    if(s != null) {
      c = new char[s.length()];
        
      for (int i = 0; i < s.length(); i++) {
        c[i] = s.charAt(i);
      }
    }
    return c;  
  }
  
  
  /**
   * Return the index of the value e
   * in the array a, or -1 if that value
   * is not in the array.
   */ 
  public static int findElement(int[] a, int e) {
    for(int i = 0; i < a.length; i++) {
      if(a[i] == e) {
        return i;
      }
    }
    return -1;
  }
  
  /**
   * Example of returning an array.
   */ 
  public static int[] returnArray(int[] a) {
    return a;
  }
  
  /**
   * Display the contents of an array a from 
   * index i to index j.
   */ 
  public static void display(int[] a, int i, int j) {
    for(int k = i; k < j; k++) {    
      System.out.println(a[k]);
    }
  }
  
  /**
   * Swap element i with element j in 
   * array a.
   */
  public static void swap(char[] a, int i, int j){
    char temp = a[i];
    a[i] = a[j];
    a[j] = temp;
  }
  
  /** 
   * Return true if array a and b are equal.
   */
  public static boolean equals(int[] a, int[] b) {
    
    // if they have the same memory address
    if(a == b) {
      return true;
    }
    
    // if one of the references is null
    if(a == null || b == null){
      return false;
    }
    
    // if the arrays are not the same length
    if(a.length != b.length) {
      return false;
    }
    
    // compare the corresponding elements of the
    // arrays
    for (int i = 0; i < a.length; i++) {
      if(a[i] != b[i]) {
        return false;
      }
    }
    
    return true;
  }
}
