Week 10 Arrays PDF
Document Details
Uploaded by JovialFallingAction3576
Saudi Electronic University
Tags
Summary
This document is a set of lecture notes on Java arrays, covering various topics such as Creating, Initializing, and Modifying arrays, Exception Handling, and Multidimensional Arrays for Object Oriented Programming.
Full Transcript
College of Computing and Informatics Object Oriented Programming Object Oriented Programming Week 10 Arrays 1. Primitive vs. Reference type 2. Creating, Initializing, and modifying arrays 3. Exception Handling (Try...
College of Computing and Informatics Object Oriented Programming Object Oriented Programming Week 10 Arrays 1. Primitive vs. Reference type 2. Creating, Initializing, and modifying arrays 3. Exception Handling (Try-Catch Statement) 4. Enhanced for Statement 5. Passing Arrays to Methods 6. Pass-By-Value vs. Pass-By-Reference 7. Multidimensional Arrays Contents Make use of Arrays to solve different Java problems Differentiate between Pass-By-Value vs. Pass-By-Reference Weekly Learning Outcomes Required Reading 1. Chapter 6 - (Java: How to program: Late Objects) Recommended Reading 1. Chapter 3 - (Java The Complete Reference, Eleventh Edition) 2.Java Arrays Tutorial: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html Primitive vs. Reference type Primitive vs. Reference type A primitive-type variable can hold exactly one value of its declared type at a time. For example, an int variable can store one integer at a time. When another value is assigned to that variable, the new value replaces the previous one. Primitive vs. Reference type Programs use variables of reference types to store the addresses of objects in the computer’s memory. creates a Scanner object, then assigns to the variable input a reference to that Scanner object. Creating, Initializing, and modifying arrays Creating, Initializing, and modifying arrays Data structures Arrays Creating, Initializing, and modifying arrays Arrays Creating, Initializing, and modifying arrays Index Also called subscript Position number in square brackets Must be positive integer or integer expression a = 5; b = 6; c[ a + b ] += 2; Adds 2 to c[ 11 ] Creating, Initializing, and modifying arrays The name of the array is c c.length returns array c’s length This array’s 12 elements are referred to as c, c, c, …, c The value of c is -45 The value of c is 6 The value of c is 0 sum = c + c + c; Creating, Initializing, and modifying arrays Declaring and Creating Arrays Array objects occupy space in memory. Arrays are created with keyword new. int[] c = new int; Equivalent to: int[] c; // declare the array variable c = new int; // create the array; assign to array variable Create several arrays in a single declaration String[] b = new String, x = new String; Creating, Initializing, and modifying arrays Creating and Initializing an Array Example Output Creating, Initializing, and modifying arrays Using an Array Initializer Output Creating, Initializing, and modifying arrays Calculating the Values to Store in an Array Output Creating, Initializing, and modifying arrays Summing the Elements of an Array Output Creating, Initializing, and modifying arrays Using Bar Charts to Display Array Data Graphically Output Creating, Initializing, and modifying arrays Using the Elements of an Array as Counters Output Exception Handling (Try-Catch Statement) Exception Handling (Try-Catch Statement) An exception indicates A problem that occurs while a program executes. To handle an exception, place any code that might throw an exception in a try-Catch statement. The try Statement contains the code that might throw an exception. The Catch Statement contains the code that handles the exception if one occurs. Exception Handling (Try-Catch Statement) Try-Catch Syntax try { // Block of code to try } catch(Exception e) { // Block of code to handle errors } Reference: https://www.w3schools.com/java/java_try_catch.asp Exception Handling (Try-Catch Statement) Exception Example public class Main { public static void main(String[ ] args) { int[] myNumbers = {1, 2, 3}; System.out.println(myNumbers); // error! } } Output Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at Main.main(Main.java:4) Reference: https://www.w3schools.com/java/java_try_catch.asp Exception Handling (Try-Catch Statement) finally statement lets you execute code, after try...catch, regardless of the result: public class Main { public static void main(String[ ] args) { try { int[] myNumbers = {1, 2, 3}; System.out.println(myNumbers); } catch (Exception e) { System.out.println("Something went wrong."); } finally { System.out.println(“The try catch is finished."); } } } Output Something went wrong. Reference: https://www.w3schools.com/java/java_try_catch.asp Exception Handling (Try-Catch Statement) Exception Example public class Main { public static void main(String[ ] args) { try { int[] myNumbers = {1, 2, 3}; System.out.println(myNumbers); } catch (Exception e) { System.out.println("Something went wrong."); } } } Output Something went wrong. Reference: https://www.w3schools.com/java/java_try_catch.asp Enhanced for Statement Enhanced for Statement Iterates through the elements of an array without using a counter. thus avoiding the possibility of “stepping outside” the array parameter has a type and an identifier arrayName is the array through which to iterate. Enhanced for Statement Output Passing Arrays to Methods Passing Arrays to Methods To pass an array argument to a method, specify the name of the array without any brackets. For example to pass the array hourlyTemperatures The method call will be: The method header will be: Passing Arrays to Methods Example: Passing Arrays to Methods Example: Output Pass-By-Value vs. Pass-By-Reference Pass-By-Value vs. Pass-By-Reference Two ways to pass arguments in method calls in many programming languages are pass-by-value (call-by-value ). pass-by-reference (call-by-reference). Pass-By-Value vs. Pass-By-Reference pass-by-value When an argument is passed by value: a copy of the argument’s value is passed to the called method. The called method works exclusively with the copy. Changes to the called method’s copy do not affect the original variable’s value in the caller Pass-By-Value vs. Pass-By-Reference pass-by-reference When an argument is passed by reference: The called method can access the argument’s value in the caller directly and modify that data, if necessary. It improves performance by eliminating the need to copy possibly large amounts of data. Pass-By-Value vs. Pass-By-Reference Java does not allow you to choose pass-by-value or pass-by- reference—all arguments are passed by value. Dealing with reference object type will be discussed in detail in later chapters. Multidimensional Arrays Multidimensional Arrays Multidimensional arrays Tables with rows and columns Two-dimensional array Declaring two-dimensional array b int b[][] = { { 1, 2 }, { 3, 4 } }; 1 and 2 initialize b and b 3 and 4 initialize b and b int b[][] = { { 1, 2 }, { 3, 4, 5 } }; row 0 contains elements 1 and 2 row 1 contains elements 3, 4 and 5 Multidimensional Arrays Multidimensional Arrays Output References Deitel, P. J., & Deitel, H. M. (2017). Java: how to program late objects. Pearson. Thank You