package ArraysEx; import java.util.Arrays; import java.util.Random; // demonstrating further usage of arrays // variable length argument lists // anonymous arrays // array utilities public class ArraysEtc { // given an array of ints, return the average public static double average(int[] list) { if (list != null) { int total = 0; // foreach loop knows how to look at each element of array for (int x : list) { total += x; } // Exactly equivalent to /* for (int i = 0; i < list.length; i++) { int x = list[i]; total+=x; } */ return (double) total / list.length; } return 0; } // given a list of ints, return their total sum // method with variable length argument list public static int addUp(int... list) { int total = 0; if (list != null) { //inside the method, list is an array of ints for (int x : list) { total += x; } } return total; } // given a list of ints, return their total sum // method with variable length argument list public static void show(String title, int... list) { System.out.print(title + " ("); if (list != null) { //inside the method, list is an array of ints for (int x : list) { System.out.print(x + " "); } } System.out.println(") "); } // example of array utilities public static void copyArrays() { // arrays to copy from int[] list1 = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int[] list2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int[][] list2D = {{1,2,3},{4,5,6},{7,8,9}}; int[][] list2D2 = {{1,2,3},{5},{7,8,9}}; String list1String = Arrays.toString(list1); // " [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]" System.out.println("list1String: " + list1String); String list2DString = Arrays.deepToString(list2D); // " [[1, 2, 3], [4, 5, 6], [7, 8, 9]]" System.out.println("list2DString: " + list2DString); // fill array all with same value int[] list3 = new int[10]; Arrays.fill(list3, 5); // [5, 5, 5, 5, 5, 5, 5, 5, 5, 5] System.out.println("filled array: " + Arrays.toString((list3))); // array to copy to int[] list1Copy; // WRONG WAY TO COPY list1Copy = list1; // both are pointing at same array // changing one changes the other // utility to copy list1Copy = Arrays.copyOf(list1, list1.length); // [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] System.out.println("copy: " + Arrays.toString(list1Copy)); // now there are two arrays with the same elements // changing one does not affect the other // copy to shorter array list1Copy = Arrays.copyOf(list1, list1.length/2); // [10, 20, 30, 40, 50] System.out.println("shorter copy: " + Arrays.toString(list1Copy)); // copy to longer array list1Copy = Arrays.copyOf(list1, list1.length*2); // [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] System.out.println("longer copy: " + Arrays.toString(list1Copy)); // this makes a copy of the array of arrays // but the elements point to the same int arrays // so changes to their elements are seen by both // can point at different int arrays without changing original int[][] list2Dcopy = Arrays.copyOf(list2D, list2D.length); list2Dcopy[0][0] = 0; // changes original list2Dcopy[2] = new int[] {7, 7, 7 }; // no effect on original System.out.println("2dArray copy: " + Arrays.deepToString(list2Dcopy));// [[0, 2, 3], [4, 5, 6], [7, 7, 7]] System.out.println("2dArray original: " + Arrays.deepToString(list2D));// [[0, 2, 3], [4, 5, 6], [7, 8, 9]] // get a fresh copy list1Copy = Arrays.copyOf(list1, list1.length); System.out.println("different arrays are same? " + Arrays.equals(list1, list2)); System.out.println("same arrays are same? " + Arrays.equals(list1, list1Copy)); // get a fresh copy list2Dcopy = new int[][] {{0,2,3},{4,5,6},{7,8,9}}; System.out.println("different 2D arrays are same? " + Arrays.deepEquals(list2D, list2D2)); // fails: different arrays with the same elements System.out.println("same 2D arrays are same? " + Arrays.equals(list2D, list2Dcopy)); // check actual elements System.out.println("same 2D arrays are same (deep)? " + Arrays.deepEquals(list2D, list2Dcopy)); } // pseudorandom numbers public static void randoms() { // Math.random() double r = Math.random();// 0.0 to 0.9999 double r1 = Math.random() * 10;// 0.0 to 9.9999 double r2 = Math.random() * 10 + 5;// 5.0 to 14.9999 int r3 = (int) (Math.random() * 100 + 1);// 1 to 100 int r4 = (int) Math.random() * 100 + 1; // always 1 !!! // Random class, need to import Random rand = new Random(); int r5 = rand.nextInt(); // any int int r6 = rand.nextInt(10); // 0..9 double r7 = rand.nextDouble(); // same as Math.random() boolean b = rand.nextBoolean(); // 50/50 } // examples of calling the above methods public static void main(String[] args) { // create array variable, then pass to method int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; System.out.println("Average: " + average(nums)); // anonymous array created in argument list // and passed to method w/o creating array variable System.out.println("Average: " + average(new int[]{2, 4, 6, 8, 10})); // we can't get at that array again, we don't have its address! // addup takes list of ints (doesn't need to be array) // of any length System.out.println("Total: " + addUp(1, 3, 5, 7, 9)); System.out.println("Total: " + addUp(1, 3, 5)); System.out.println("Total: " + addUp(1, 3)); System.out.println("Total: " + addUp()); // array of length 0, not null // addup will also accept actual array of ints System.out.println("Total: " + addUp(nums)); show("Some Numbers", 1, 2, 5, 6, 9, 0); show("No Numbers"); show("Nums Numbers", nums); // average accepts arrays, but not lists -- compile error // System.out.println("Total: " + average(1,3,5,7,9)); copyArrays(); } }