java
play

Java: Learning to Program with Robots Chapter 10: Arrays Chapter - PowerPoint PPT Presentation

Java: Learning to Program with Robots Chapter 10: Arrays Chapter Objectives After studying this chapter, you should be able to: Store data in an array, access a single element, process all elements, search for a particular element, and put


  1. Java: Learning to Program with Robots Chapter 10: Arrays

  2. Chapter Objectives After studying this chapter, you should be able to: • Store data in an array, access a single element, process all elements, search for a particular element, and put the elements in order • Declare, allocate, and initialize an array • Handle changing numbers of elements in an array, including inserting a new element and deleting an existing one • Enlarge or shrink the size of an array • Manipulate data stored in a multi-dimensional array

  3. 10.1 Using Arrays (1/2)

  4. 10.1: Using Arrays (2/2) Wanted: • A way to easily work with 1,500 (or many more!) persons collected from www.myspace.com . • Each person represented as an instance of the Person class: Person -String name -int id -Gender gender -int age -String city -String province -String country -DateTime lastLogon -??? friends +Person(Scanner in) +String getName( ) +int getID( ) +Gender getGender( ) ...

  5. 10.1.1: Visualizing An Array persons Steve, MALE, 16, St. Cat Person[ ] Ken, MALE, 18, Niagara-on length 8 Beth, FEMALE, 17, St. Cat [0] Kathleen, FEMALE, 23, Tim [1] [2] Roydyn, MALE, 21, Fonth [3] Kala, FEMALE, 18, Niaga [4] Ali, MALE, 19, Boston, MS [5] [6] Zaki, FEMALE, 21, Kitch [7]

  6. 10.1.2: Accessing One Array Element Person p3 = new Person(…); … persons … // An array of Person objects System.out.println(p3.getName()); System.out.println(persons[3].getName); if (p3.getGender() == Gender.FEMALE) { System.out.println(p3.getName() + " is female." ); } if (persons[3].getGender() == Gender.FEMALE) { System.out.println(persons[3].getName() + " is female." ); } persons[8].addFriend(p3); p3.addFriend(persons[8]); p3 = new Person(…); persons[3] = new Person(…);

  7. 10.1.3: Swapping Array Elements (1/2) public class PersonList extends Object { … persons … /** Swap the person object at index a with the object at index b. */ public void swap(int a, int b) { Person temp = this.persons[a]; this.persons[a] = this.persons[b]; this.persons[b] = temp; } } Assume that swap(1, 2) has been called. Person temp = this.persons[a]; temp persons Person[ ] Steve, MALE, 16, St. Cat length 4 Ken, MALE, 18, Niagara-on [0] [1] Beth, FEMALE, 17, St. Cat [2] Kathleen, FEMALE, 23, Tim [3] (trace continued on next slide)

  8. 10.1.3: Swapping Array Elements (2/2) this.persons[a] = this.persons[b]; temp persons Person[ ] Steve, MALE, 16, St. Cat length 4 Ken, MALE, 18, Niagara-on [0] [1] Beth, FEMALE, 17, St. Cat [2] Kathleen, FEMALE, 23, Tim [3] this.persons[b] = temp; temp persons Person[ ] Steve, MALE, 16, St. Cat length 4 Ken, MALE, 18, Niagara-on [0] [1] Beth, FEMALE, 17, St. Cat [2] Kathleen, FEMALE, 23, Tim [3] // After the swap method finishes persons Person[ ] Steve, MALE, 16, St. Cat length 4 Ken, MALE, 18, Niagara-on [0] [1] Beth, FEMALE, 17, St. Cat [2]

  9. 10.1.4: Processing All the Elements public class PersonList extends Object { … persons … /* Print the name and number of friends for every person in the array. */ public void printBasicInfo() { for(int i = 0; i < this.persons.length; i += 1) { Person p = this.persons[ i ]; System.out.println(p.getName() + " has " + p.getNumFriends + "friends" ); } } /** Calculate the average number of friends */ public double calcAverageNumberOfFriends() { int sumFriends = 0; for( int i = 0; i < this.persons.length; i += 1) { Person p = this.persons[i]; sumFriends = sumFriends + p.getNumFriends(); } return (double) sumFriends / this.persons.length; } }

  10. 10.1.4: Processing All Elements with ForEach public class PersonList extends Object { … persons … /** Calculate the average number of friends */ public double calcAverageNumberOfFriends() { int sumFriends = 0; for( int i = 0; i < this.persons.length; i += 1) { Person p = this.persons[i]; sumFriends = sumFriends + p.getNumFriends(); } return (double) sumFriends / this.persons.length; } /** Calculate the average number of friends using a “foreach loop” */ public double calcAverageNumberOfFriends() { int sumFriends = 0; for( Person p : this.persons ) { sumFriends = sumFriends + p.getNumFriends(); } return (double) sumFriends / this.persons.length; } }

  11. 10.1.5: Processing Matching Elements for ( each element in the array ) { if ( the element meets some criteria ) { process the element } } public class PersonList extends Object { … persons … /** Count the number of minors (persons less than 18 years old). */ public int countMinors() { int count = 0; for(int i = 0; i < this.persons.length; i += 1) { if (this.persons[i].getAge() < 18) { count += 1; } } return count; } }

  12. 10.1.6: Searching (1/2) Searching uses some identifying information – name, telephone number, ID number – to find the corresponding object in the array. The identifying information is often called the key . Find the person with ID 107733. public class PersonList extends Object { … persons … /** Find the person with the given id; null if not found. */ public Person search(int id) { for(int i=0; i<this.persons.length; i+= 1) { Person p = this.persons[i]; if (p.getID() == id) { return p; // success! Return, exiting loop. } } return null; // not found } }

  13. 10.1.6: Searching (2/2) One way of structuring the search is to ask when we’re done: • Found the correct element (success!) • Reached the end of the list (failure) public class PersonList extends Object { … persons … /** Find the person with the given id; null if not found. Use a traditional loop */ public Person search2(int id) { int i = 0; while (i < this.persons.length && this.persons[i].getID() != id) { i += 1; } Person answer = null; if (i < this.persons.length) { answer = this.persons[i]; } return answer; } }

  14. 10.1.7: Finding an Extreme Element (1/2) An extreme element has the most of something or the least of something. The most age, the longest time since the last login, the “smallest” name (first in dictionary order), etc. remember the first element as the best seen so far for ( each remaining element in the array ) { if ( the current element is better than the best seen so far ) { remember the current element as the best seen so far } } return best element seen so far

  15. 10.1.7: Finding an Extreme Element (2/2) public class PersonList extends Object { … persons … /** Find the youngest person. */ public Person findYoungestPerson() { Person youngestSoFar = this.persons[0]; for(Person currentPerson : this.persons) { if (currentPerson.getAge() < youngestSoFar.getAge()) { youngestSoFar = currentPerson; } } return youngestSoFar; } } persons Person[ ] Steve, MALE, 20, St. Cat Quick Quiz length 4 Ken, MALE, 18, Niagara What does [0] [1] Beth, FEMALE, 18, St. Cat findYoungestPerson [2] return for the array Kathleen, FEMALE, 23, [3] shown on the right?

  16. 10.1.8: Sorting an Array (Ideas 1/3) Sorting an array puts all the elements in order by age, name, or some other criteria. Selection Sort is one of many algorithms to sort an array. It builds on three patterns we’ve already seen: • Process All Elements • Find an Extreme • Swap Two Elements We’ll sort the persons array by name. persons persons F aizel, MALE, 16, St. Ca F aizel, MALE, 16, St. Cat Person[ ] Person[ ] E llen , FEMALE, 18, Niaga E llen , FEMALE, 18, Niaga length length 7 7 [0] [0] A li, MALE, 19, Boston, A li, MALE, 19, Boston, MS [1] [1] G reg, MALE, 21, Fonth G reg, MALE, 21, Fonth [2] [2] [3] [3] B eth, FEMALE, 17, St. B eth, FEMALE, 17, St. C [4] [4] D oug, MALE, 18, Niagara- D oug, MALE, 18, Niagara-on [5] [5] [6] [6] C athy, FEMALE, 23, Tim C athy, FEMALE, 23, Tim F E A G B D C A B C D E F G

  17. 10.1.8: Sorting an Array (Ideas 2/3) Divide the array into the part that’s already sorted (dark background) and the part that isn’t (light background). 0 1 2 3 4 5 6 A B C G E D F Repeatedly extend the sorted part of the array by: • Finding the smallest element in the unsorted part of the array. 0 1 2 3 4 5 6 A B C G E D F • Swapping it with the first element in the unsorted part of the array. 0 1 2 3 4 5 6 A B C D E G F • Extending the sorted part of the array. 0 1 2 3 4 5 6 A B C D E G F

  18. 10.1.8: Sorting an Array (Ideas 3/3) 0 1 2 3 4 5 6 The initial, unsorted array. F E A G B D C Find the element that belongs at index 0. F E A G B D C Swap elements at 0 and 2, extending sorted part. A E F G B D C Find the element that belongs at index 1. A E F G B D C Swap elements at 1 and 4, extending sorted part. A B F G E D C Find the element that belongs at index 2. A B F G E D C Swap elements at 2 and 6, extending sorted part. A B C G E D F Find the element that belongs at index 3. A B C G E D F Swap elements at 3 and 5, extending sorted part. A B C D E G F Find the element that belongs at index 4. A B C D E G F Swap elements at 4 and 4, extending sorted part. A B C D E G F Find the element that belongs at index 5. A B C D E G F Swap elements at 5 and 6, extending sorted part. A B C D E F G for ( each position in the array except the last ) { find the element that should go in this position swap that element with the element currently there }

Recommend


More recommend