/* * ArrayExample.java * example of using primitive array to store grades */ package cis119.pkg15arrays; public class ArrayExample { // dealing with lists of values the stupid way public static void howNotToDoIt() { // how not to do it int grade0 = 67; int grade1 = 71; int grade2 = 99; int grade3 = 83; int grade4 = 77; int grade5 = 88; int grade6 = 62; int grade7 = 79; int grade8 = 91; int grade9 = 86; double average = 0; average += grade0; average += grade1; average += grade2; average += grade3; average += grade4; average += grade5; average += grade6; average += grade7; average += grade8; average += grade9; average = average / 10; System.out.println("Grades:"); System.out.println("\thwk 1: " + grade0); System.out.println("\thwk 2: " + grade1); System.out.println("\thwk 3: " + grade2); System.out.println("\thwk 4: " + grade3); System.out.println("\thwk 5: " + grade4); System.out.println("\thwk 6: " + grade5); System.out.println("\thwk 7: " + grade6); System.out.println("\thwk 8: " + grade7); System.out.println("\thwk 9: " + grade8); System.out.println("\thwk 10: " + grade9); System.out.println("average: " + average); // lost the will to code... } // dealing with lists of values with arrays public static void withArrays() { // grades is an array of ints (not just one) int [] grades; // at this point, grades is null // grades will contain 10 ints // this means grade.length is 10 // and grades goes from grades[0] to grades[9] // grades holds the address of an array of ints grades = new int[10]; // individual values, so we still have to do this // manually, one by one. grades[0] = 67; grades[1] = 71; grades[2] = 99; grades[3] = 83; grades[4] = 77; grades[5] = 88; grades[6] = 62; grades[7] = 79; grades[8] = 91; grades[9] = 86; // but we do have a shortcut grades = new int[]{67, 71, 99, 83, 77, 88, 62, 79, 91, 86}; double average = 0; // using a loop to add up all the elements of the array for (int i = 0; i < grades.length; i++){ // first time through the loop grades[i] is grades[0] // then grades[1] then grades[2] etc up to grades[9] average += grades[i]; } average = average / grades.length; // using a loop to print all the elements of the array System.out.println("Grades:"); for (int i = 0; i < grades.length; i++){ System.out.println("\thwk "+ (i + 1) + ": " + grades[i]); } System.out.println("average: " + average); } // call the methods, demo more arrays public static void main(String[] args) { howNotToDoIt(); withArrays(); // other arrays double [] dlist = new double[5]; String [] words = new String[10]; boolean [] yesNo = new boolean[4]; // doubles in arrays act like any other double dlist[0] = 2.2/3.0; dlist[1] = dlist[0] * 2; dlist[2] = dlist[0] + dlist[1]; //dlist = 18.5; // NO, dlist is an ARRAY, not a double // Strings in arrays act like any other String words[5] = "hello"; words[3] = "wor" + "ld"; words[0] = words[5] + " " + words[3]; //words = "quick brown fox"; // NO, words is an ARRAY, not a String // booleans in arrays act like any other boolean yesNo[0] = true; yesNo[1] = 5 < 19; yesNo[3] = yesNo[0] && yesNo[1]; //yesNo = false; // NO, yesNo is an ARRAY, not a boolean // parallel arrays of carrots and names that some bunnies have int[] carrots = new int[10]; // special initialization for names String[] names = new String[] {"Mopsy", "Flopsy", "Cottontail", "Peter", "Bugs", "Inky", "Blinky", "Stinky", "Winky", "Clyde"}; // could just do = {"Mopsy", .. etc // but this works more often // zeroth bunny should have 10 carrots, 1th should hav 13, 2nd 16, 3rd 19 etc int val = 10; // starting value for carrots // assign all bunnies carrots for (int i = 0; i < carrots.length; i++) { carrots[i] = val + 3 * i; // original value plus the right number of 3s } // print info about the bunnies for (int i = 0; i < names.length; i++) { // humans number from 1, so adjust the number i to i+1 System.out.println((i + 1) + ". " + names[i] + " has " + carrots[i] + " carrots"); } } }