// 11/20/2019

/** print an array -- use a for loop
  * copy an array -- first allocate new memory and then use for loop
  * compare 2 arrays
  */
public class ArrayProcessing {
  public static void main(String[] args) {
   
    int[] numbers = {99, 97, 96, 99, 98, 97, 96};
    
    // int[] sortedNums = {1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4};
    // to print an array, loop and print each value
    for (int num : numbers)
       System.out.println(num); 
    // I want a copy of the array in numbers2
    // array assignment copies ADDRESSES, so you end up 
    // with 2 names for the same array. Changes to numbers2 will
    // change numbers array and vice versa.
    int[] numbers2 = new int[numbers.length]; // allocates the array
    for (int i=0; i<numbers.length; i++) {
      numbers2[i] = numbers[i]; // copy elt by elt
    }
    if (numbers2==numbers)
      System.out.println("addresses to not equal.");
    numbers[0]++;
    compare(numbers, numbers2); 
    
  }
  /** method to compare 2 arrays
   *  are the two arrays identical?
   * @param arr1
   * @param arr2
   * @return true if the array are identical
   */
  public static void compare(int[] arr1, int[] arr2) {
    if (arr1.length != arr2.length) {
      System.out.println("arrays are not equal");
      return;
    }
    /* ATTEMPT #1 NOT good. prints a msg for each loc 
    for (int i=0; i<arr1.length; i++) {
      // compare corresponding elements
      if (arr1[i] == arr2[i])
        System.out.println("arrays are equal");
      else
        System.out.println("arrays are NOT equal");
    }
    */
    // ATTEMPT #2 -- GOOD
    for (int i=0; i<arr1.length; i++) {
      // compare corresponding elements
      if (arr1[i] != arr2[i]) {
        System.out.println("arrays are not equal");
        return;
      }
    } // fell out of loop
    System.out.println("arrays are equal");
  }
  
  // ATTEMPT #3 GOOD (use a boolean var) gets rid of return inside of the loop
  public static boolean compare2(int[] arr1, int[] arr2) {
    if (arr1.length != arr2.length) {
      return false;
    }
    boolean same = true;
    for (int i=0; i<arr1.length; i++) {
      // compare corresponding elements
      if (arr1[i] != arr2[i]) {
        same = false;
      }
    } // fell out of loop
    return same;
  }
}




