1 / 9

Manipulating Data Structures with Java Collections

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.

plilly
Download Presentation

Manipulating Data Structures with Java Collections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS 270—Application Development II Chapter 19—Collections

  2. 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 _______.

  3. 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.

  4. 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.).

  5. 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 _________.

  6. 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.

  7. 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.

  8. 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”.

  9. 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).

More Related