Agenda Array accessing Automatic initialization of an array Array exceptions Array parameter Enhanced for loop presentations
Array –accessing an element An array element is accessed by including its index in brackets after the array name. System.out.println(friends[2]); //displays ?
Array – changing an element If we want to change the Friends array, element 1 to “Sunshine”, how? Friends[0] = “Sunshine”;
Automatic initialization of an array Only with numeric array(double or int), all elements are auto initialized to 0. double[] doubleArray= new double[10]; But not with String array
Which of the following correctly initializes an array arr to contain 4 elements, each with value 0? Int [] arr = {0, 0, 0, 0}; Int[] arr = new int[4]; for (int i=0; i < arr; i++) arr [i] = 0;
What is exception? Errors that are not detected by the compiler may generate a run-time error. A run-time error, also called an exception, halts program execution at the statement that cannot be executed.
Array run time errors NullPointerException double x[]; => declared only x[0] = 3.5; => error ArrayIndexOutOfBoundException A run-time error when an invalid index is used.
Run time error for array Friends[4] = “Mercury”; => ? A run-time error is generated when an invalid index is used. For example, the error(exception) ArrayIndexOutOfboundsException is thrown when the following statement tries to execute:
Parameters passing to methods Pass by reference - Arrays, objectsd Pass by value - primitive data types
Pass by reference int[] list = { 1, 2, 3, 4}; changeArray(list); System.out.print(“The changed list is “); for(int i=0; i< list.length; i++) System.out.print(list[i] + “ “);
Pass by reference list 3 4 list 3 4 b list 6 7 b list 6 7 Before the method call: 1 22 3 4 list At start of the method call: 1 22 3 4 b list Just before exiting the method: 4 5 6 7 b After exiting the method: list 4 5 6 7
Pass by value public static void tryChanging(int aNum) { aNum = 456; } int origNum = 123; tryChanging(origNum);
Pass by Value Before the method call: origNum 123 At start of the method call: origNum 123 aNum 123 origNum Just before exiting the method: 123 aNum 456 After exiting the method: origNum 123
Method with array and int params public static void tryChanging(int[] numbers, int aNum) { numbers[1] = 123; aNum = 456; }
int[] myNums = {5, 8, 3}; System. out int[] myNums = {5, 8, 3}; System.out.println(myNums[1] + " " + myNums[0]); tryChanging(myNums, myNums[0]);
Enhanced for loop Auto loop through all the elements of an array. Done without specifying the length of the array and without any dummy integer index. Syntax for(type dummyName: Array name) dummyName is read-only
Example of enhanced for loop int[] aArry = {1, 2, 3, 4}; for (int aElem : aArry) System.out.println(aElem+ " "); => For each element in the aArry, print it out = for(int i=0; i < aArry.length; i++) System.out.println(aArry[i] + “ “);
Practice Exercise 1 —EvensAndOdds Create an EvensAndOdds application that generates 25 random integers between 0 and 99 and then displays all the evens on one line and all the odds on the next line. Application output should look similar to: ODD: 21 21 87 39 45 7 75 87 9 27 Even: 4 36 44 98 0 40 82 32 0 26
Practice Exercise 2 Palindrome Create a Palindrome application that prompts the user for a string and then displays a message indicating whether or not the string is a palindrome. A palindrome is a word or phrase that is spelled the same backwards and forwards. For example, “mom” is a palindrome, as well as “kayak” and “Never odd or even”. Turn in the exercise 1 before the break Turn in the exercise 2 for extra bonus
Presentations Algorithm – written in pseudo code, your approach for the problem. Flowchart – a diagram representations. Source code – should be bug free and has output as intended