Chapter 11 Arrays Continued Fundamentals of Java Objectives 2 Use string methods appropriately. Write a method for searching an array. Understand why a sorted array can be searched more efficiently than an unsorted array. Write a method to sort an array. Fundamentals of Java Objectives (cont.) 3 Write methods to perform insertions and removals at given positions in an array. Understand the issues involved when working with arrays of objects. Perform simple operations with Java’s ArrayList class. Fundamentals of Java Vocabulary 4 Array list Binary search Bubble sort Immutable object Insertion sort Fundamentals of Java Vocabulary (cont.) 5 Linear search Selection sort Substring Wrapper class Fundamentals of Java Advanced Operations on Strings Most text-processing applications examine and manipulate the characters in strings. – – – String objects are immutable. – 6 Separating strings into segments Searching for/replacing specific characters or substrings Inserting text into a string No mutators in the String class Fundamentals of Java Advanced Operations on Strings (cont.) 7 Table 11-1: Some commonly used String methods Fundamentals of Java Advanced Operations on Strings (cont.) 8 Table 11-1: Some commonly used String methods (cont.) Fundamentals of Java Advanced Operations on Strings (cont.) 9 Table 11-1: Some commonly used String methods (cont.) Fundamentals of Java Advanced Operations on Strings (cont.) 10 Example 11.2: Count the words and compute the average word length in a sentence. Fundamentals of Java Advanced Operations on Strings (cont.) 11 Example 11.2: Count the words and compute the average word length in a sentence (cont.). Fundamentals of Java Searching 12 Linear search: Search a data structure (such as an array) from beginning to end Searching an array of objects: Fundamentals of Java Searching (cont.) Binary search: An efficient search algorithm based on eliminating half of the data from the search at each iteration – – Data must be sorted first. Examine midpoint of data, then decide which half of the data to continue searching on. Discard 13 other half of data. Fundamentals of Java Searching (cont.) 14 Binary search code: Fundamentals of Java Searching (cont.) Figure 11-1: Trace of a binary search of an array 15 Fundamentals of Java Searching (cont.) To compare objects, best if the class implements the Comparable interface – compareTo method Table 11-2: Behavior of the method compareTo 16 Fundamentals of Java Searching (cont.) 17 Binary search for objects: Fundamentals of Java Searching (cont.) 18 Implementing a Comparable class example: Fundamentals of Java Sorting Arranging the elements of a collection of data (such as an array) in an ordered fashion Figure 11-2: Array before and after sorting 19 Fundamentals of Java Sorting: Selection Sort 20 Basic idea: Table 11-3: Trace of data during a selection sort Fundamentals of Java Sorting: Selection Sort (cont.) 21 Must be able to find smallest number in an array and swap items in an array Fundamentals of Java Sorting: Bubble Sort Pass through array comparing adjacent elements – 22 If out of order, swap. Table 11-4: Trace of data during one pass of a bubble sort Fundamentals of Java Sorting: Bubble Sort (cont.) Pseudocode: Fewer data exchanges than selection sort – 23 Sort can stop early if array already sorted Fundamentals of Java Sorting: Insertion Sort After kth pass of sorting loop (k starting at 1), first k items should be in sorted order. Table 11-4: Trace of data during an insertion sort 24 Fundamentals of Java Sorting: Insertion Sort (cont.) 25 Pseduocode: Fundamentals of Java Sorting (cont.) Any of the search algorithms can be altered to support sorting of objects. – Object’s class(es) should implement Comparable 26 Have compareTo method Example: Fundamentals of Java Insertions and Removals Steps for insertion: – – 1. Check for available space. 2. Check validity of target index. Between – – – 27 0 and logical size 3. Shift items from logical end of array to target index down by one position. 4. Assign new item to cell at target index. 5. Increment logical size by one. Fundamentals of Java Insertions and Removals (cont.) Figure 11-3: Inserting an item into an array 28 Fundamentals of Java Insertions and Removals (cont.) Steps for removal: – 1. Check validity of target index. Between – – 29 0 and logical size 2. Shift items from target index to logical end of array up by one position. 3. Decrement logical size by one. Fundamentals of Java Insertions and Removals (cont.) Figure 11-4: Removing an item from an array 30 Fundamentals of Java Working with Arrays of Objects When array type is an interface type, abstract class, or superclass of 1+ other classes, array may contain different object types. – 31 Might not all respond to common set of messages Fundamentals of Java Working with Arrays of Objects (cont.) What if you want to perform an operation specific to one of the types in the array? – Most general arrays have type Object. – 32 Can use the instanceOf operator to determine the specific type of element in the array Can hold any type of object Fundamentals of Java The Class java.util.ArrayList Contains sequence of elements ordered by position – Unlike an array in that: It uses methods rather than [] to manipulate elements. It tracks the logical size and physical size. The logical size is 0 when created. – 33 Size automatically adjusted as needed The positions available for access range from 0 to the logical size minus 1. Fundamentals of Java The Class java.util.ArrayList (cont.) 34 Generic array list: Programmer must specify element type for the list Raw array list: Can contain objects of any reference type Declaring/instantiating a generic array list: Fundamentals of Java The Class java.util.ArrayList (cont.) 35 Table 11-6: Some commonly used ArrayList methods Fundamentals of Java The Class java.util.ArrayList (cont.) ArrayList objects cannot directly store primitive types. – Must use wrapper classes Classes that contain the value of a primitive type Boolean, Integer, Double, Character 36 Fundamentals of Java The Class java.util.ArrayList (cont.) 37 ArrayList objects automatically “box” and “unbox” primitive values when used with ArrayList methods. Fundamentals of Java The Class java.util.ArrayList (cont.) Advantages of ArrayList over arrays: – – 38 Includes many methods for tasks such as insertions, removals, and searches Tracks own logical size and grows or shrinks automatically with the number of elements contained in it Fundamentals of Java Graphics and GUIs: Menus A drop-down menu system consists of a menu bar, a number of menus, and several selections for each menu. – – – – 39 May have sub-menus Menu item object for each menu selection (class JMenuItem) Menu object for each menu (class JMenu) Menu bar object in which all of the menu objects will appear (class JMenuBar) Fundamentals of Java Graphics and GUIs: Menus (cont.) Listener objects are attached to menus. – When menu items are selected, events are fired and the listener objects respond. Figure 11-6: New user interface for the student test scores program 40 Fundamentals of Java Graphics and GUIs: Menus (cont.) Example 11.6: TestScoresView class (with menus) 41 Fundamentals of Java Graphics and GUIs: Menus (Cont.) Example 11.6: TestScoresView class (with menus, cont.) 42 Fundamentals of Java Summary 43 Linear search: Simple search that works well for small- and medium-sized arrays Binary search: Clever search that works well for large arrays but assumes that the elements are sorted Comparisons of objects are accomplished by implementing the Comparable interface, which requires the compareTo method. Fundamentals of Java Summary (cont.) 44 Selection sort, bubble sort, and insertion sort are simple sort methods that work well for small- and medium-sized arrays. Insertions and removals of elements at arbitrary positions are complex operations that require careful design and implementation. Fundamentals of Java Summary (cont.) 45 One can insert objects of any class into an array of Object. When retrieved from the array, objects must be cast down to their classes before sending them most messages. The limitation of a fixed-size array can be overcome by using Java’s ArrayList class. Fundamentals of Java Summary (cont.) 46 An array list tracks and updates its logical size and provides many useful client methods. Wrapper class, such as Integer, provides a way of packaging a value of a primitive type, such as int, in an object so that it can be stored in an array of Object or an array list. Fundamentals of Java