Arrays of Objects - COMP 248 Object-Oriented Programming I PDF
Document Details
Uploaded by EntrancedSerendipity1806
Willamette University
Tags
Summary
These are lecture notes from Willamette University on arrays of objects in Java. They cover how arrays work in Java, passing them as parameters, returning arrays, arrays of objects and other examples.
Full Transcript
COMP 248 OBJECT ORIENTED PROGRAMMING I http://www.willamette.edu/~gorr/classes/cs231/lectures/chapter9/ Chapter 6 – Part B: Arrays of Objects 1 Chapter 6 – Part B 1. Arrays are objects 2. Array...
COMP 248 OBJECT ORIENTED PROGRAMMING I http://www.willamette.edu/~gorr/classes/cs231/lectures/chapter9/ Chapter 6 – Part B: Arrays of Objects 1 Chapter 6 – Part B 1. Arrays are objects 2. Arrays of objects not covered from chap. 6: – enumerated types – methods with variable number of parameters 2 Arrays The elements of an array can be: – a primitive type or – an object reference In Java, the array itself is an object – so the name of the array is a reference – and the array itself must be created with new 3 1- Arrays are objects In Java, an array is an object – so an array can be passed as a parameter to a method Like any other object: – the reference to the array is passed (much faster) – so the formal and actual parameters become aliases of each other – so changing an array element within the method changes the original object passing the entire array ≠ passing a single element of the array 4 Example public boolean isVowel(char someChar) { } public int nbVowel(char[] someCharArray) { } char[] alphabet = {'a', 'b', 'c', 'd, …, 'z'}; if (isVowel(alphabet)) // ok ? System.out.print(“…"); if (isVowel(alphabet)) // ok? System.out.print("…"); if (nbVowel(alphabet)>3) // ok? System.out.print("…"); if (nbVowel(alphabet)>3) // ok? System.out.print("…"); 5 Example Write a method that will return the sum of an array of integers. 6 Arrays and assignment we cannot change the size of an array but we can assign an array to another… int[] a1 = {10, 20, 30}; int[] a2 = {1, 2, 3, 4, 5}; System.out.println(a1.length); System.out.println(a1); a1 = a2; System.out.println(a1.length); System.out.println(a1); Output 7 Duplicating/Coping an array to copy the content of an array into another: static int[] duplicate(int[] theOriginal) { int[] theCopy = new int[theOriginal.length]; for (int i = 0; i < theOriginal.length; i++) theCopy[i] = theOriginal[i]; return theCopy; } or use the built-in clone method (for 1-d arrays) theCopy = (int[]) theOriginal.clone(); note: clone returns an array to a generic Object, we need to cast the result to what we want (ex. int[]) 8 Example public static void main(String[] args) { int[] a1 = {10, 20, 30}; int[] a2 = {1, 2, 3, 4, 5}; with: a1 = a2; System.out.println(a1); with: a1 = duplicate(a2); // a1 = a2; // a1 = duplicate(a2); // a1 = (int[]) a2.clone(); with: a1 = (int[]) a2.clone(); System.out.println(a1); a1 = 99; System.out.println(a1); Output System.out.println(a2); } public static int[] duplicate(int[] theOriginal) { int[] theCopy = new int[theOriginal.length]; for (int i = 0; i < theOriginal.length; i++) theCopy[i] = theOriginal[i]; return theCopy; } 9 Methods That Return an Array a method may also return an array public static int[] incrementArray(int[] a, int increment) { int[] temp = new int[a.length]; int i; for (i = 0; i < a.length; i++) temp[i] = a[i] + increment; return temp; } 10 Privacy Leaks with Array Instance Variables If an accessor returns the contents of an array, special care must be taken – Just as when an accessor returns a reference to any private object public double[] getArray() { return anArray;//BAD! privacy leak } 11 Privacy Leaks with Array Instance Variables The method should return a reference to a deep copy of the private array object – public double[] getArray() { double[] temp = new double[count]; for (int i = 0; i < count; i++) temp[i] = a[i]; return temp } 12 2- Arrays of objects we can have arrays of primitive types 1 2 10 6 25 'a' 'd' 'e' 'z' 'f' we can have arrays of objects (more precisely, arrays of references to objects) 13 ex: Arrays of strings ex: 5 references to String objects String[] words = {"I", "am", "very", "hungry", "!"}; “I” “am” … … “!” 14 Arrays of objects 1. create the array of references Account[] bank = new Account; 2. create each object bank = new Account("ted", 111, 100); bank = new Account("mary", 222, 500); bank = new Account("john", 999, 5); Each object in the array must be created individually 3. manipulate each object for (int i = 0; i < bank.length; i++) { bank[i].deposit(100)); System.out.print(bank[i].getBalance()); } 15 Example Declare the class Airplane with speed, nbPassengers, Pilot. Declare an array of 100 Airplane objects 16 Example write a method that takes an array of Airplane and returns the average speed of the Airplane. If the array is empty, return 0. 17 Example public class Telephone { private int number; public Telephone(int number) { this.number = number; } public String ring() { return "dring dring"; } } Declare a 2-D array of 10x10 Telephone objects. Initialize each of the 100 telephone objects to the phone number (514) 848-2424. Call the method ring on all 100 elements of the array and display the result. 18