Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.

Similar presentations


Presentation on theme: "Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators."— Presentation transcript:

1 Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators

2 © 2006 Pearson Addison-Wesley. All rights reserved16-2 Reading Assignment: Chapter 16 from the pdf file attached

3 © 2006 Pearson Addison-Wesley. All rights reserved16-3 Collections A Java collection is any class that holds objects and implements the Collection interface –For example, the ArrayList class is a Java collection class, and implements all the methods in the Collection interface –Collections are used along with iterators The Collection interface is the highest level of Java's framework for collection classes –All of the collection classes discussed here can be found in package java.util

4 © 2006 Pearson Addison-Wesley. All rights reserved16-4 The Collection Landscape

5 © 2006 Pearson Addison-Wesley. All rights reserved16-5 Wildcards Classes and interfaces in the collection framework can have parameter type specifications that do not fully specify the type plugged in for the type parameter –Because they specify a wide range of argument types, they are known as wildcards public void method(String arg1, ArrayList arg2) –In the above example, the first argument is of type String, while the second argument can be an ArrayList with any base type

6 © 2006 Pearson Addison-Wesley. All rights reserved16-6 Wildcards A bound can be placed on a wildcard specifying that the type used must be an ancestor type or descendent type of some class or interface –The notation specifies that the argument plugged in be an object of any descendent class of String –The notation specifies that the argument plugged in be an object of any ancestor class of String

7 © 2006 Pearson Addison-Wesley. All rights reserved16-7 The Collection Framework The Collection interface describes the basic operations that all collection classes should implement –The method headings for these operations are shown on the next several slides Since an interface is a type, any method can be defined with a parameter of type Collection –That parameter can be filled with an argument that is an object of any class in the collection framework

8 © 2006 Pearson Addison-Wesley. All rights reserved16-8 Method Headings in the Collection Interface ( Part 1 of 10)

9 © 2006 Pearson Addison-Wesley. All rights reserved16-9 Method Headings in the Collection Interface ( Part 2 of 10)

10 © 2006 Pearson Addison-Wesley. All rights reserved16-10 Method Headings in the Collection Interface ( Part 3 of 10)

11 © 2006 Pearson Addison-Wesley. All rights reserved16-11 Method Headings in the Collection Interface ( Part 4 of 10)

12 © 2006 Pearson Addison-Wesley. All rights reserved16-12 Method Headings in the Collection Interface ( Part 5 of 10)

13 © 2006 Pearson Addison-Wesley. All rights reserved16-13 Method Headings in the Collection Interface ( Part 6 of 10)

14 © 2006 Pearson Addison-Wesley. All rights reserved16-14 Method Headings in the Collection Interface ( Part 7 of 10)

15 © 2006 Pearson Addison-Wesley. All rights reserved16-15 Method Headings in the Collection Interface ( Part 8 of 10)

16 © 2006 Pearson Addison-Wesley. All rights reserved16-16 Method Headings in the Collection Interface ( Part 9 of 10)

17 © 2006 Pearson Addison-Wesley. All rights reserved16-17 Method Headings in the Collection Interface ( Part 10 of 10)

18 © 2006 Pearson Addison-Wesley. All rights reserved16-18 Collection Relationships There are a number of different predefined classes that implement the Collection interface –Programmer defined classes can implement it also A method written to manipulate a parameter of type Collection will work for all of these classes, either singly or intermixed There are two main interfaces that extend the Collection interface: The Set interface and the List interface

19 © 2006 Pearson Addison-Wesley. All rights reserved16-19 Collection Relationships Classes that implement the Set interface do not allow an element in the class to occur more than once –The Set interface has the same method headings as the Collection interface, but in some cases the semantics (intended meanings) are different –Methods that are optional in the Collection interface are required in the Set interface

20 © 2006 Pearson Addison-Wesley. All rights reserved16-20 Collection Relationships Classes that implement the List interface have their elements ordered as on a list –Elements are indexed starting with zero –A class that implements the List interface allows elements to occur more than once –The List interface has more method headings than the Collection interface –Some of the methods inherited from the Collection interface have different semantics in the List interface –The ArrayList class implements the List interface

21 © 2006 Pearson Addison-Wesley. All rights reserved16-21 Methods in the Set Interface (Part 1 of 10)

22 © 2006 Pearson Addison-Wesley. All rights reserved16-22 Methods in the Set Interface (Part 2 of 10)

23 © 2006 Pearson Addison-Wesley. All rights reserved16-23 Methods in the Set Interface (Part 3 of 10)

24 © 2006 Pearson Addison-Wesley. All rights reserved16-24 Methods in the Set Interface (Part 4 of 10)

25 © 2006 Pearson Addison-Wesley. All rights reserved16-25 Methods in the Set Interface (Part 5 of 10)

26 © 2006 Pearson Addison-Wesley. All rights reserved16-26 Methods in the Set Interface (Part 6 of 10)

27 © 2006 Pearson Addison-Wesley. All rights reserved16-27 Methods in the Set Interface (Part 7 of 10)

28 © 2006 Pearson Addison-Wesley. All rights reserved16-28 Methods in the Set Interface (Part 8 of 10)

29 © 2006 Pearson Addison-Wesley. All rights reserved16-29 Methods in the Set Interface (Part 9 of 10)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

47 © 2006 Pearson Addison-Wesley. All rights reserved16-47 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

48 © 2006 Pearson Addison-Wesley. All rights reserved16-48 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

49 © 2006 Pearson Addison-Wesley. All rights reserved16-49 Concrete Collections Classes The ArrayList and Vector classes implement the List interface, and can be used if additional methods are not needed –Both the ArrayList and Vector classes implement all the methods in the interface List –Either class can be used when a List with efficient random access to elements is needed

50 © 2006 Pearson Addison-Wesley. All rights reserved16-50 Concrete Collections Classes The concrete class LinkedList is a concrete derived class of the abstract class AbstractSequentialList –When efficient sequential movement through a list is needed, the LinkedList class should be used

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

66 © 2006 Pearson Addison-Wesley. All rights reserved16-66 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

67 © 2006 Pearson Addison-Wesley. All rights reserved16-67 Pitfall: Omitting the When the or corresponding class name is omitted from a reference to a collection class, this is an error for which the compiler may or may not issue an error message (depending on the details of the code), and even if it does, the error message may be quite strange –Look for a missing or when a program that uses collection classes gets a strange error message or doesn't run correctly

68 © 2006 Pearson Addison-Wesley. All rights reserved16-68 Iterators An iterator is an object that is used with a collection to provide sequential access to the collection elements –This access allows examination and possible modification of the elements An iterator imposes an ordering on the elements of a collection even if the collection itself does not impose any order on the elements it contains –If the collection does impose an ordering on its elements, then the iterator will use the same ordering

69 © 2006 Pearson Addison-Wesley. All rights reserved16-69 The Iterator Interface Java provides an Iterator interface –Any object of any class that satisfies the Iterator interface is an Iterator An Iterator does not stand on its own –It must be associated with some collection object using the method iterator –If c is an instance of a collection class, the following can be used to visit all the elements of c : Iterator iter = c.iterator(); while(iter.hasNext( )){ Object obj = iter.next(); process(obj); }

70 © 2006 Pearson Addison-Wesley. All rights reserved16-70 Methods in the Iterator Interface (Part 1 of 2) Note: When next() is invoked, the iterator jumps over the next element, and it returns a reference to the object that it just passed.

71 © 2006 Pearson Addison-Wesley. All rights reserved16-71 Methods in the Iterator Interface (Part 2 of 2) Note: It is illegal to call remove() of an iterator if it was not preceded by a call to next().

72 © 2006 Pearson Addison-Wesley. All rights reserved16-72 Using an Iterator with an ArrayList Object (1/2) 1.import java.util.*; 2.public class TestIterator { 3. public static void main(String[] args) { 4. ArrayList list = new ArrayList (); 5. Iterator iter; 6. for(int i = 0; i < 6; i++) 7. list.add(new Integer(i)); 8. 9. System.out.println("list before: "+list); 10. 11. iter = list.iterator(); 12. if (iter.hasNext()) { 13. iter.next(); 14. } 15. if (iter.hasNext()) { 16. iter.next(); 17. } 18. iter.remove(); 19. System.out.println("list after first removal: "+list);

73 © 2006 Pearson Addison-Wesley. All rights reserved16-73 Using an Iterator with an ArrayList Object (2/2) 20./* 21. // The following will compile, but will raise an Exception 22. iter.remove(); 23. // It had to be preceded by an iter.next() 24.*/ 25. 26. iter = list.iterator(); // Have to “reset” the iterator 27. 28. while (iter.hasNext()) { 29. Integer element = (Integer)iter.next(); 30. System.out.println(element); 31. } 32. } 33.}

74 © 2006 Pearson Addison-Wesley. All rights reserved16-74 Tip: For-Each Loops as Iterators Although it is not an iterator, a for-each loop can serve the same purpose as an iterator –A for-each loop can be used to cycle through each element in a collection For-each loops can be used with any of the collections discussed here

75 © 2006 Pearson Addison-Wesley. All rights reserved16-75 Example 1.import java.util.*; 2.public class ForEachDemo2 { 3. public static void main(String[] args) { 4. ArrayList list = new ArrayList (); 5. for(int i = 0; i < 6; i++) 6. list.add(new Integer(i)); 7. 8. System.out.println("list before: "+list); 9. 10. for (Integer element : list) { 11. System.out.println(element); 12. } 13. } 14.}

76 © 2006 Pearson Addison-Wesley. All rights reserved16-76 The ListIterator Interface The ListIterator interface extends the Iterator interface, and is designed to work with collections that satisfy the List interface –A ListIterator has all the methods that an Iterator has, plus additional methods –A ListIterator can move in either direction along a list of elements –A ListIterator has methods, such as set and add, that can be used to modify elements

77 © 2006 Pearson Addison-Wesley. All rights reserved16-77 Methods in the ListIterator Interface (Part 1 of 4)

78 © 2006 Pearson Addison-Wesley. All rights reserved16-78 Methods in the ListIterator Interface (Part 2 of 4)

79 © 2006 Pearson Addison-Wesley. All rights reserved16-79 Methods in the ListIterator Interface (Part 3 of 4)

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

81 © 2006 Pearson Addison-Wesley. All rights reserved16-81 The ListIterator Cursor Every ListIterator has a position marker known as the cursor –If the list has n elements, they are numbered by indices 0 through n-1, but there are n+1 cursor positions –When next() is invoked, the element immediately following the cursor position is returned and the cursor is moved forward one cursor position –When previous() is invoked, the element immediately before the cursor position is returned and the cursor is moved back one cursor position

82 © 2006 Pearson Addison-Wesley. All rights reserved16-82 ListIterator Cursor Positions

83 © 2006 Pearson Addison-Wesley. All rights reserved16-83 Pitfall: next and previous Can Return a Reference Theoretically, when an iterator operation returns an element of the collection, it might return a copy or clone of the element, or it might return a reference to the element Iterators for the standard predefined collection classes, such as ArrayList and HashSet, actually return references –Therefore, modifying the returned value will modify the element in the collection

84 © 2006 Pearson Addison-Wesley. All rights reserved16-84 An Iterator Returns a Reference (Part 1 of 4)

85 © 2006 Pearson Addison-Wesley. All rights reserved16-85 An Iterator Returns a Reference (Part 2 of 4)

86 © 2006 Pearson Addison-Wesley. All rights reserved16-86 An Iterator Returns a Reference (Part 3 of 4)

87 © 2006 Pearson Addison-Wesley. All rights reserved16-87 An Iterator Returns a Reference (Part 4 of 4)


Download ppt "Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators."

Similar presentations


Ads by Google