/* ArrayExamples.java example code AC Chapin * * primitive arrays * arrays as parameters * returning arrays * variable length parameter lists * random */ package Arrays; import java.util.Random; public class ArrayExamples { // arrays using primitives public static void intArrays() { // an int int x; x = 5; // an array of ints; int[] numList; // currently null numList = new int[5]; numList[0] = x; numList[1] = 12; numList[2] = numList[0] + numList[1]; // numList[37] = 100; // index out of bounds error //for loop sets array element i to be x*i // use numList.length for safety for (int i = 0; i < numList.length; i++) { numList[i] = x * i; } // special initialization // at declaration can leave out new int[] numList2 = {11, 22, 33, 44}; // to do later, need new numList2 = new int[] {44, 33, 22, 11}; // combine -- careful not to spill off end of either array int[] sumList = new int[numList.length]; for (int i = 0; i < numList.length && i < numList2.length; i++) { sumList[i] = numList[i] + numList2[i]; } } // arrays using objects public static void bunnyArrays() { // a Bunny Bunny b; // currently null b = new Bunny("Bugs", 5); //(name, carrotcount) // an array of Bunnys Bunny[] warren; // null warren = new Bunny[10]; // array exists, Bunnys in array are null // array elements as variables warren[0] = new Bunny("Hazel", 27); warren[2] = b; warren[4] = warren[0]; warren[6] = new Bunny("Buster", 17); warren[7] = new Bunny("Roger", 31); warren[9] = new Bunny("Babs", 5); warren[4].setName("Bigwig"); // warren[0] is same address System.out.println("zeroth rabbit:" + warren[0].getName()); // warren[1].setName("Peter Cottontail"); // null pointer error // pass array to method that returns Bunny with most carrots Bunny mostCarrots = findMostCarrots(warren); // print info about max Bunny if (mostCarrots != null) { System.out.println(mostCarrots.getName() + " has " + mostCarrots.getCarrotCount() + " carrots"); } else { System.out.println("No Bunnys"); } } // method with array parameter // return Bunny from the array who has most carrots public static Bunny findMostCarrots(Bunny[] bunList) { Bunny most = null; for (int i = 0; i < bunList.length; i++) { // first Bunny we find in array is our first guess for most carrots if (most == null) { most = bunList[i]; // when we find a bunny, compare carrot counts // with best we have seen so far } else if (bunList[i] != null) { // if current Bunny has more carrots than previous max // current Bunny becomes max if (bunList[i].getCarrotCount() > most.getCarrotCount()) { most = bunList[i]; } } } return most; } // passing and returning arrays // given array of ints // create array of strings spelling out content of int array public static String[] makeStringList(int[] numbers) { // no array passed in if (numbers == null) { return null; } // create array String[] strList = new String[numbers.length]; for (int i = 0; i < strList.length; i++) { strList[i] = "Int Array[" + i + "] = " + numbers[i]; } return strList; } // 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 } // main calls other methods public static void main(String[] args) { intArrays(); bunnyArrays(); int[] nums = {98, 87, 76, 65, 54}; // method that returns array of strings // pass in array of ints -- note NO SQUARE BRACES String[] strs = makeStringList(nums); System.out.println(strs[1]); System.out.println(strs[3]); // makeStringList accepts arrays, but not lists -- compile error //String[] badstrs = makeStringList(1, 2, 5, 6, 9, 0); randoms(); } }