/** * Examples of array initialization, output, changing values, exceptions * * @author William Albritton */ public class InitializingDisplayingArrays { /** * The "main" Method Starts The Program. * * @param commandlineArguments Are Not Used. */ public static void main(String[] commandlineArguments) { /* Usually we create a constant for the array size. The size of the array is the number of elements in the array. */ final Integer SIZE = new Integer(7); /* Syntax to Declare and Instatiate An Array: Class[] arrayName = new Class[numberOfElements]; Declare: creating a variable (a box) to hold data. In this case, the variable will hold the address to an object. Syntax to declare an array: Class[] arrayName Instantiate: creating an object, which will store the data of the object. Syntax to instantiate an array: new Class[numberOfElements] The following code declares and instantiates an array of Integers. The address to the array is stored in variable examScores. */ Integer[] examScores = new Integer[SIZE]; //declare and instantiate an array of Strings: String letterGrades[] = new String[SIZE]; /* Display An Array by looping from 0th to SIZE-1 for the index. Index: the position of the element in the array. Element: what is contained at each index in the array. */ System.out.println("exam scores: "); System.out.println("index(subscript) value(element)"); for (int i = 0; i < SIZE; i++) { System.out.println(" " + i + " " + examScores[i]); } System.out.println(); /* Syntax to initialize each element of an array: arrayName[index] = new Class(); To initialize each element of an array, we must assign each element to a value, otherwise the elements are "null" (nothing). */ examScores[0] = new Integer(80); examScores[1] = new Integer(77); examScores[2] = new Integer(92); examScores[3] = new Integer(80); examScores[4] = new Integer(63); examScores[5] = new Integer(95); examScores[5] = new Integer(95); examScores[6] = new Integer(55); /* Display An Array again using printf() method for formatting. Note that "null" was replaced by an address to an object. The object contains the integer value. */ System.out.println("exam scores: "); System.out.println("index(subscript) value(element)"); for (int i = 0; i < examScores.length; i++) { //System.out.println(" " + i + " " + examScores[i]); /* Syntax for printf(): printf("%NumberLetter1 %NumberLetter2 %NumberLetterN\n", variable1, variable2, variableeN); " " - what is printed out is put between the double quotes number - field width, which is the minimum number of spaces reserved for the output d - is used to display an integer f - is used to display a floating point s - is used to display a String \n - is the newline , - put commas between each variable following the double-quotes */ System.out.printf("%6d %15d\n", i, examScores[i]); } System.out.println(); /* Change all the values of the array elements. The syntax depends on how you want to change the element. Syntax to assign a new value: arrayName[i] = newValue; Syntax to re-use the same value: arrayName[i] = arrayName[i] operator newValue; In this loop, we add 2 points to each exam score. */ for(int i = 0; i < SIZE; i++){ examScores[i] = examScores[i] + 2; } /* Assign values to an array inside a loop. Depending on the grade, we can set the letter grade with a for-loop. A is 90 and above B is between 90 and 80 C is between 80 and 70 D is between 70 and 60 F is below 60 These are added to the array letterGrades[] */ for(int i = 0; i < SIZE; i++) { if(100 >= examScores[i] && 90 <= examScores[i]){ letterGrades[i] = "A"; } else if(89 >= examScores[i] && 80 <= examScores[i]){ letterGrades[i] = "B"; } else if(79 >= examScores[i] && 70 <= examScores[i]){ letterGrades[i] = "C"; } else if(69 >= examScores[i] && 60 <= examScores[i]){ letterGrades[i] = "D"; } else if(59 >= examScores[i] && 0 <= examScores[i]){ letterGrades[i] = "F"; } else{ letterGrades[i] = "ERROR!"; } } // Print Both Arrays using printf() method for formatting System.out.println("exam scores letter grade"); for(int i = 0; i < SIZE; i++) { //System.out.println(" " + examScores[i] + " " + letterGrades[i]); /* Notes on printf() s - is used to output Strings */ System.out.printf("%7d %14s\n", examScores[i], letterGrades[i]); } System.out.println(); /* Out Of Bounds Error example. This happens because "less than or equals" is used: i <= examScores.length (instead of using "less than": i < examScores.length) When looping through the index numbers of an array, we should NOT loop into index numbers for elements that do not exist. Note: You should NOT do this in your code. This is an example of what NOT to do. This shows you how a try-catch block works. */ try { for (int i = 0; i <= examScores.length; i++) { System.out.print(examScores[i] + ", "); } } catch (ArrayIndexOutOfBoundsException exception) { String message = exception.toString(); System.out.println(message); message = "ERROR: The array went out of bounds!"; System.out.println(message); } } } /* exam scores: index(subscript) value(element) 0 null 1 null 2 null 3 null 4 null 5 null 6 null exam scores: index(subscript) value(element) 0 80 1 77 2 92 3 80 4 63 5 95 6 55 exam scores letter grade 82 B 79 C 94 A 82 B 65 C 97 A 57 D 82, 79, 94, 82, 65, 97, 57, java.lang.ArrayIndexOutOfBoundsException: 7 ERROR: The array went out of bounds! */