Download presentation
Presentation is loading. Please wait.
Published byAldous Moody Modified over 6 years ago
1
Agenda Array accessing Automatic initialization of an array
Array exceptions Array parameter Enhanced for loop presentations
2
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 ?
3
Array – changing an element
If we want to change the Friends array, element 1 to “Sunshine”, how? Friends[0] = “Sunshine”;
4
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
5
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;
6
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.
7
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.
8
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:
9
Parameters passing to methods
Pass by reference - Arrays, objectsd Pass by value - primitive data types
10
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] + “ “);
11
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
12
Pass by value public static void tryChanging(int aNum) { aNum = 456; }
int origNum = 123; tryChanging(origNum);
13
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
14
Method with array and int params
public static void tryChanging(int[] numbers, int aNum) { numbers[1] = 123; aNum = 456; }
15
int[] myNums = {5, 8, 3}; System. out
int[] myNums = {5, 8, 3}; System.out.println(myNums[1] + " " + myNums[0]); tryChanging(myNums, myNums[0]);
17
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
18
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] + “ “);
19
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: Even:
20
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
21
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.