// class demonstrating use of arrays public class ArrayExamples { /* * ArrayExamples.java example code */ package cis214.pkg08arrays; 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 int[] numList2 = {11, 22, 33, 44}; // 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; } // 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 to method array of ints -- note NO SQUARE BRACES String[] strs = makeStringList(nums); System.out.println(strs[1]); System.out.println(strs[3]); } } }