Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1101: Programming Methodology Recitation 6 – Arrays and Collections.

Similar presentations


Presentation on theme: "CS1101: Programming Methodology Recitation 6 – Arrays and Collections."— Presentation transcript:

1 CS1101: Programming Methodology Recitation 6 – Arrays and Collections

2 2 Passing Arrays to Methods Array is a reference data type, similar to an object When an array is passed to a method, only its reference is passed. A copy of the array is NOT created in the method.

3 public int searchMin (double[] number) { int indexMin = 0; for (int i = 1; i < number.length; i++) { if (number[i] < number[indexMin]) { indexMin = i; } return indexMin; } Method returns index of smallest element in an array. Array to search for is passed to method double[] arrayReal; //create and assign values to arrayReal … //get index of smallest element in array int min = searchMin (arrayReal); //output result System.out.println (“Minimum value in ArrayReal is ” + arrayReal[min] + “ at position ” + min); arrayRealnumber Value of arrayReal, which is a reference to an array is copied to number. Both variables refer to same array.

4 public double[] readDoubles () { double[] number; System.out.println (“How many input values?”); int N = Integer.parseInt(stdin.readLine()); number = new double[N]; for (int i = 0; i < N; i++) { number[i] = Double.parseDouble(stdin.readLine()); } return number; } Method reads in values and returns values as an array of double - Actually returns the reference to the array of double double[] arrayOne, arrayTwo; //create and assign values to the arrays arrayOne = readDoubles(); arrayTwo = readDoubles(); Method readDoubles() creates a new array. We do not have to create an array on the calling side

5 5 Case Study Let us design a class called AddressBook to maintain a collection of Person objects. Use an array to implement AddressBook class An AddressBook object will allow a programmer to add, delete or search for a Person object in the address book.

6 public class AddressBook { private static final int DEFAULT_SIZE = 25; private Person[] entry;//array of Person objects private int count;//number of elements in array, //which is also the position to add //the next Person object. public AddressBook () { this (DEFAULT_SIZE); } public AddressBook (int size) { count = 0; entry = new Person[size]; } Constructors for AddressBook class

7 The add Method - accept a Person object as parameter and add Person object to array - what happens when there is no more space in array ? 1. return false if array is full. Otherwise return true 2. increase array size public void add (Person newPerson) { if (count == entry.length) {//no more space left, enlarge();// create a new larger array } entry[count] = newPerson;//at this point, entry refers to a new larger array count++; } private void enlarge() { //create a new array whose size is 150% of current array int newLength = (int) (1.5 * entry.length); Person[] temp = new Person[newLength]; for (int i = 0; i < entry.length; i++) { //copy data to new array temp[i] = entry[i]; } entry = temp;//set variable entry to point to new array }

8 The search Method - returns all Person objects that match the search criteria - use ArrayList for resizeable list representation to return multiple Person objects public ArrayList search (int searchAge) { ArrayList foundPersons; foundPersons = new ArrayList(); for (int i = 0; i < count; i++) { if (entry[i].getAge() == searchAge ) { foundPersons.add (entry[i].clone()); } return foundPersons; } An empty list with initial capacity of 10. Capacity increase automatically. Exercise: The delete Method

9 public class testAddressBook { public static void main (String[] args) { AddressBook myBook; Person person; myBook = new AddressBook (5); for (int i = 0; i < 5; i++) {// add 5 Person objects person = new Person (“Ms. X” + i, 10, ‘F’); myBook.add (person); } person = new Person (“Mr Y”, 15, ‘M’); // add the 6 th person myBook.add (person);// a new array is created ArrayList friends = myBook.search(10);// search for all the 10 yr olds for (int i = 0; i < friends.size(); i++) {// display the names of 10 yr olds person = (Person) friends.get(i); System.out.println (person.getName()); }


Download ppt "CS1101: Programming Methodology Recitation 6 – Arrays and Collections."

Similar presentations


Ads by Google