Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)

Similar presentations


Presentation on theme: "© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)"— Presentation transcript:

1 © 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)

2 © 2006 Pearson Addison-Wesley. All rights reserved16-2 Methods in the List Interface (Part 2 of 16)

3 © 2006 Pearson Addison-Wesley. All rights reserved16-3 Methods in the List Interface (Part 3 of 16)

4 © 2006 Pearson Addison-Wesley. All rights reserved16-4 Methods in the List Interface (Part 4 of 16)

5 © 2006 Pearson Addison-Wesley. All rights reserved16-5 Methods in the List Interface (Part 5 of 16)

6 © 2006 Pearson Addison-Wesley. All rights reserved16-6 Methods in the List Interface (Part 6 of 16)

7 © 2006 Pearson Addison-Wesley. All rights reserved16-7 Methods in the List Interface (Part 7 of 16)

8 © 2006 Pearson Addison-Wesley. All rights reserved16-8 Methods in the List Interface (Part 8 of 16)

9 © 2006 Pearson Addison-Wesley. All rights reserved16-9 Methods in the List Interface (Part 9 of 16)

10 © 2006 Pearson Addison-Wesley. All rights reserved16-10 Methods in the List Interface (Part 10 of 16)

11 © 2006 Pearson Addison-Wesley. All rights reserved16-11 Methods in the List Interface (Part 11 of 16)

12 © 2006 Pearson Addison-Wesley. All rights reserved16-12 Methods in the List Interface (Part 12 of 16)

13 © 2006 Pearson Addison-Wesley. All rights reserved16-13 Methods in the List Interface (Part 13 of 16)

14 © 2006 Pearson Addison-Wesley. All rights reserved16-14 Methods in the List Interface (Part 14 of 16)

15 © 2006 Pearson Addison-Wesley. All rights reserved16-15 Methods in the List Interface (Part 15 of 16)

16 © 2006 Pearson Addison-Wesley. All rights reserved16-16 Methods in the List Interface (Part 16 of 16)

17 © 2006 Pearson Addison-Wesley. All rights reserved16-17 Pitfall: Optional Operations When an interface lists a method as "optional," it must still be implemented in a class that implements the interface –The optional part means that it is permitted to write a method that does not completely implement its intended semantics –However, if a trivial implementation is given, then the method body should throw an UnsupportedOperationException

18 © 2006 Pearson Addison-Wesley. All rights reserved16-18 Tip: Dealing with All Those Exceptions The tables of methods for the various collection interfaces and classes indicate that certain exceptions are thrown –These are unchecked exceptions, so they are useful for debugging, but need not be declared or caught In an existing collection class, they can be viewed as run-time error messages In a derived class of some other collection class, most or all of them will be inherited In a collection class defined from scratch, if it is to implement a collection interface, then it should throw the exceptions that are specified in the interface

19 © 2006 Pearson Addison-Wesley. All rights reserved16-19 Concrete Collections Classes The concrete class HashSet implements the Set interface, and can be used if additional methods are not needed –The HashSet class implements all the methods in the Set interface, and adds only constructors –The HashSet class is implemented using a hash table The ArrayList and Vector classes implement the List interface, and can be used if additional methods are not needed –Both the ArrayList and Vector interfaces implement all the methods in the interface List –Either class can be used when a List with efficient random access to elements is needed

20 © 2006 Pearson Addison-Wesley. All rights reserved16-20 Concrete Collections Classes The concrete class LinkedList is a concrete derived class of the abstract class AbstractList LinkedList class is another concrete class, which uses a private linked list for its elements instead of an array The interface SortedSet and the concrete class TreeSet are designed for implementations of the Set interface that provide for rapid retrieval of elements –The implementation of the class is similar to a binary tree, but with ways to do inserting that keep the tree balanced

21 © 2006 Pearson Addison-Wesley. All rights reserved16-21 Methods in the HashSet Class (Part 1 of 2)

22 © 2006 Pearson Addison-Wesley. All rights reserved16-22 Methods in the HashSet Class (Part 2 of 2)

23 © 2006 Pearson Addison-Wesley. All rights reserved16-23 Methods in the Classes ArrayList and Vector (Part 1 of 15)

24 © 2006 Pearson Addison-Wesley. All rights reserved16-24 Methods in the Classes ArrayList and Vector (Part 2 of 15)

25 © 2006 Pearson Addison-Wesley. All rights reserved16-25 Methods in the Classes ArrayList and Vector (Part 3 of 15)

26 © 2006 Pearson Addison-Wesley. All rights reserved16-26 Methods in the Classes ArrayList and Vector (Part 4 of 15)

27 © 2006 Pearson Addison-Wesley. All rights reserved16-27 Methods in the Classes ArrayList and Vector (Part 5 of 15)

28 © 2006 Pearson Addison-Wesley. All rights reserved16-28 Methods in the Classes ArrayList and Vector (Part 6 of 15)

29 © 2006 Pearson Addison-Wesley. All rights reserved16-29 Methods in the Classes ArrayList and Vector (Part 7 of 15)

30 © 2006 Pearson Addison-Wesley. All rights reserved16-30 Methods in the Classes ArrayList and Vector (Part 8 of 15)

31 © 2006 Pearson Addison-Wesley. All rights reserved16-31 Methods in the Classes ArrayList and Vector (Part 9 of 15)

32 © 2006 Pearson Addison-Wesley. All rights reserved16-32 Methods in the Classes ArrayList and Vector (Part 10 of 15)

33 © 2006 Pearson Addison-Wesley. All rights reserved16-33 Methods in the Classes ArrayList and Vector (Part 11 of 15)

34 © 2006 Pearson Addison-Wesley. All rights reserved16-34 Methods in the Classes ArrayList and Vector (Part 12 of 15)

35 © 2006 Pearson Addison-Wesley. All rights reserved16-35 Methods in the Classes ArrayList and Vector (Part 13 of 15)

36 © 2006 Pearson Addison-Wesley. All rights reserved16-36 Methods in the Classes ArrayList and Vector (Part 14 of 15)

37 © 2006 Pearson Addison-Wesley. All rights reserved16-37 Methods in the Classes ArrayList and Vector (Part 15 of 15)

38 © 2006 Pearson Addison-Wesley. All rights reserved16-38 Differences Between ArrayList and Vector For most purposes, the ArrayList and Vector are equivalent –The Vector class is older, and had to be retrofitted with extra method names to make it fit into the collection framework –The ArrayList class is newer, and was created as part of the Java collection framework –The ArrayList class is supposedly more efficient than the Vector class also

39 © 2006 Pearson Addison-Wesley. All rights reserved16-39 The Collections Class The Collections class is a utility class. It contains static methods for manipulating collection objects. The Arrays class is also a utility class. It contains static methods for manipulating arrays. One useful method used is asList() to convert an array into a list. The following are the most frequently used methods of the Collections class. –static int binarySearch(List list, Object o) –static int binarySearch(List list, Object o, Comparator c) –static void sort(List list) –static void sort(List list, Comparator c) –static Object max(Collection c) –static Object min(Collection c) –static void reverse(List list) –static void shuffle(List list)


Download ppt "© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)"

Similar presentations


Ads by Google