/*
 * This class illustrates programming with arrays in Java
 *
 * Nadeem Abdul Hamid, CSC120A, Fall 2004
 */

public class Arrays {
    
    public static void main(String[] args) {
        int[] arA = { 2, 4, 6 };
        int[] arB = new int[3];
        arB[0] = 2;
        arB[1] = 4;
        arB[2] = 6;
        
        /* COMPARING ARRAYS */
        
        if (arA == arB) 
            System.out.println("arA and arB are ==");
        else 
            System.out.println("arA and arB are not ==");
        
        if (compareArrays(arA, arB))
            System.out.println("(the elements of) arA and arB are equal");
        
        
        /* COPYING ARRAYS */
        //        int[] arA = { 2, 4, 6 };
        int[] arC = arA;        // make a copy of the arA array (??)
        System.out.println(arA[0]);   
        arC[0] = 10;            // change this element in the copy
        System.out.println(arA[0]);   // arA's element changed too!
        
        arA[0] = 2;             // restore arA's element
        System.out.println(arA[0]);   
        arC = copyArray(arA);   // try again
        arC[0] = 10;
        System.out.println(arA[0]);
        
        
        /* PASSING ARRAYS AS ARGUMENTS TO METHODS */
        
        //        int[] arA = { 2, 4, 6 };
        int a = 5;
        System.out.println("a: " + a);
        foo(a);
        System.out.println("a: " + a);

        System.out.println("arA[0]: " + arA[0]);
        bar(arA);        
        System.out.println("arA[0]: " + arA[0]);
        
        arA[0] = 2;
        System.out.println("arA[0]: " + arA[0]);
        foobar(arA);
        System.out.println("arA[0]: " + arA[0]);
        
        
        /* COMMAND LINE ARGUMENTS */
        
        if (args.length == 0) {
            System.out.println("No command line arguments.");
        } else {
            for (int i = 0; i < args.length; i++) {
                System.out.println("args[" + i + "]: " + args[i]);
            }
        }
        
        
        /* SORTING */
        
        int[] toSort = { 45, 19, 21, 32, 18, 4, 36, 7, 2, 12 };
        sortArray(toSort);
        for (int i = 0; i < toSort.length; i++) {
            System.out.print("  " + toSort[i]);
        }
        System.out.println();
        
        
    }
    
    
    public static void foobar(int[] ar) {
        ar = new int[10];
        for (int i = 0; i < 10; i++) {
            ar[i] = 300;
        }
    }
    
    public static void bar(int[] ar) {
        ar[0] = 10;
    }
    
    
    public static void foo(int b) {
        b = 10;
    }
    
    
    /* this method "clones" an array */
    public static int[] copyArray(int[] ar) {
        int[] copy = new int[ar.length];
        for (int i = 0; i < ar.length; i++) {
            copy[i] = ar[i];
        }
        return copy;        
    }
    
    
    /* this methods compares the contents of two arrays for
       equality */
    public static boolean compareArrays(int[] arA, int[] arB) {
        if (arA.length != arB.length)
            return false;
        // at this point we know the lengths are the same
        for (int i = 0; i < arA.length; i++) {
            if (arA[i] != arB[i]) 
                return false;
        }
        // we didn't find any discrepancies, so...
        return true;
    }
    
    
    /* this method sorts an integer array using the "selection sort"
       algorithm */
    public static void sortArray(int[] ar) {
        for (int i = 0; i < ar.length; i++) {  // finding the i'th smallest element
            int sel = i;
            for (int j = i+1; j < ar.length; j++) {
                if (ar[j] < ar[sel]) sel = j;
            }                
            if (sel != i) {       // minor optimization
                int temp = ar[i];
                ar[i] = ar[sel];
                ar[sel] = temp;   // swap ar[i] and ar[sel]
            }
        }
    }
}