90 likes | 105 Views
Learn how to manipulate data structures like arrays, linked lists, stacks, and queues using Java Collections. Explore high-level methods for sorting, searching, and comparing elements.
E N D
CIS 270—Application Development II Chapter 19—Collections
19.1-2 Introduction & Overview • You can manipulate various data structures with very “____ level” program code (see Chapter 17). • The term data __________ refers to a scheme for organizing related pieces of information. • Examples of data structures include arrays, linked lists (data linked in a chain), stacks, and queues. • The Java collections __________ contains prepackaged data structures, interfaces, and algorithms for manipulating data structures. • A collection is an object that holds references to other objects, usually of the same _______.
19.3 Class Arrays • Class Arrays provides high-level _______ methods sort, binarySearch, equals, and fill. • These methods are overloaded for primitive-type arrays and Object arrays. • Methods sort and binarySearch are overloaded with _________ versions. • See Fig. 19.2 for an example of using the class Arrays.
19.4 Interface Collection and Class Collections • Interface Collection contains _____ operations (apply to the entire collection) for adding, clearing, comparing and retaining collection elements (as well as other operations). • A Collection can be converted to an _______. • Interface Collection also contains static methods for sorting and searching, and iterating (walking through a collection). • Class Collections provides static methods that manipulate collections _______________ (for searching, sorting, etc.).
19.5 Lists • A List (a.k.a., a sequence) is an ordered Collection that can contain duplicate elements. • List indices are ______ based. • Interface List is implemented by classes such as ArrayList, LinkedList, and Vector. • Class ArrayList and Vector are resizable-array implementations of List. • Objects of class Vector are ___________ by default, those of ArrayList are not (yielding better performance). • Synchronization is only important if a collection is shared among different program _________.
19.5.1-3 Sample Programs • These sections contain sample programs that use classes ArrayList, LinkedList, and Vector. • A string array object that can hold ten strings is declared as follows: • String [] aStrArray = new String [10]; • An ArrayList object that can hold any number of strings is declared as follows: • List <String> aStrArrayList = new ArrayList <String>(); • An LinkedList object can have insertions or __________ made anywhere in the list. • A Vector object is like an ArrayList object but synchronization is the _________ for Vector.
19.6.1 Algorithm sort • To sort a List you must implement the Comparable __________. • The sort is performed using the class’s compareTo method, but alternative ordering can be performed. • Create a new comparator class (called NewComparator) that implements ____________ specifying a class whose objects will be sorted. • Create a compare method in the new comparator class to specify how comparisons will be made. • Create the list and call the sort method: Collections.sort(list, new NewComparator()); • See Fig. 19.10-11.
19.6.2-3 Other Collection Algorithms • The following apply to a List: • Algorithm shuffle ___________ orders a List’s elements. • Algorithm reverse reverses the order of elements. • Algorithm fill overwrites elements with a specified value. • Algorithm copy copies the elements of a shorter list to a list of the same size or longer. • Algorithms min and max apply to a Collection. • These algorithms can use a ____________ object that defines “large” and “small”.
19.6.4-5 Algorithm binarySearch and Others • A List object must first be ________ in ascending order before it is searched with binarySearch. • The method call binarySearch has two arguments, a List object, then an Object to be found. • If the object is found, binarySearch returns the ________ for its location in the list. • If the object is not found, binarySearch returns a negative number (see Fig. 19.14). • Algorithms addAll, frequency, and disjoint are new in J2SE ____ (see Fig. 19.15).