Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists and the Collection Interface Chapter 4 Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.

Similar presentations


Presentation on theme: "Lists and the Collection Interface Chapter 4 Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface."— Presentation transcript:

1

2 Lists and the Collection Interface Chapter 4

3 Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface To understand how to write an array-based implementation of the List interface To study the difference between single-, double-, and circular linked list data structures To learn how to implement the List interface using a linked-list To understand the Iterator interface

4 Chapter 4: Lists and the Collection Interface3 Chapter Objective (continued) To learn how to implement the iterator for a linked list To become familiar with the Java Collection framework

5 Chapter 4: Lists and the Collection Interface4 The List Interface and ArrayList Class An array is an indexed structure: can select its elements in arbitrary order using a subscript value Elements may be accessed in sequence using a loop that increments the subscript Disadvantages of arrays: You cannot Increase or decrease the length Add an element at a specified position without shifting the other elements to make room Remove an element at a specified position without shifting other elements to fill in the resulting gap

6 Chapter 4: Lists and the Collection Interface5 The List Interface and ArrayList Class (continued) Allowed operations on the List interface include: Finding a specified target Adding an element to either end Removing an item from either end Traversing the list structure without a subscript Not all classes perform the allowed operations with the same degree of efficiency An array provides the ability to store primitive-type data whereas the List classes all store references to Objects. Autoboxing facilitates this.

7 Chapter 4: Lists and the Collection Interface6 The List Interface and ArrayList Class (continued)

8 Chapter 4: Lists and the Collection Interface7 The ArrayList Class Simplest class that implements the List interface “Improvement” over an array object Used when a programmer wants to add new elements to the end of a list but still needs the capability to access the elements stored in the list in arbitrary order For example: List myList = new ArrayList (); myList.add(“Bashful”); myList.add(“Awful”); myList.add(“Jumpy”); myList.add(“Happy”);

9 Chapter 4: Lists and the Collection Interface8 The ArrayList Class Then After myList.add(2, “Doc”); After myList.add(“Dopey”);

10 Chapter 4: Lists and the Collection Interface9 The ArrayList Class (continued) Suppose we have Then myList.remove(1); And myList.set(2, “Sneezy”);

11 Chapter 4: Lists and the Collection Interface10 Generic Collections Language feature introduced in Java 5.0 called generic collections (or generics) Generics allow you to define a collection that contains references to objects of one specific type List myList = new ArrayList (); specifies that myList is a List of String where String is a type parameter Only references to objects of type String can be stored in myList, and all items retrieved are of type String. A type parameter is analogous to a method parameter.

12 Chapter 4: Lists and the Collection Interface11 Generic Collections In Java, a non-generic collection is more general than a generic collection Non-generic collection can store objects of different data types In a generic collection, all objects must be of same type

13 Chapter 4: Lists and the Collection Interface12 Creating a Generic Collection

14 Chapter 4: Lists and the Collection Interface13 Non-generic Example For example ArrayList yourList = new ArrayList(); yourList.add(new Integer(35));// element 0 is Integer yourList.add(“bunny”); // element 1 is String yourList.add(new Double(3.14));// element 2 is Double Will this work? String animal = yourList.get(1); Syntax error: incompatible types, since it is an Object Must cast: String animal = (String) yourList.get(1); Consider: String aTwo = (String) yourList.get(2); Will compile, but gives runtime error… This is bad!

15 Chapter 4: Lists and the Collection Interface14 Generics Advantages include… Type incompatibility detected at compile time Downcasting not necessary Specific data types (e.g., String), not Object The book likes generics Book suggests you only use non-generics if need to store objects of different types in same collection

16 Chapter 4: Lists and the Collection Interface15 Specification of the ArrayList Class

17 Chapter 4: Lists and the Collection Interface16 Application of ArrayList The ArrayList gives you additional capability beyond what an array provides Combining Autoboxing with Generic Collections you can store and retrieve primitive data types when working with an ArrayList Section 4.2 gives some examples Phone Directory using ArrayList instead of array Read Section 4.2 It’s only 2 pages!

18 Chapter 4: Lists and the Collection Interface17 KWArrayList Class Authors implement their our own ArrayList class Call it KWArrayList Why are they doing this? Illustrates how class actually works (not in every detail, but the basic ideas) Can analyze the work involved This work is all hidden when using ArrayList

19 Chapter 4: Lists and the Collection Interface18 KWArrayList Class KWArrayList is author’s implementation of an ArrayList class Physical size of array indicated by data field capacity Number of data items indicated by the data field size

20 Chapter 4: Lists and the Collection Interface19 KWArrayList Class Definition public class KWArrayList { // Data Fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The underlying data array */ private E[] theData; /** The current size */ private int size = 0; /** The current capacity */ private int capacity = 0;.....

21 Chapter 4: Lists and the Collection Interface20 KWArrayList Class Constructor public KWArrayList() { capacity = INITIAL_CAPACITY; theData = (E[]) new Object[capacity]; } Note that theData = (E[]) new Object[capacity]; causes compiler to issue warning message: KWListArray.java uses unchecked or unsafe operations Why?

22 Chapter 4: Lists and the Collection Interface21 KWArrayList Class Constructor (Again) public KWArrayList() { capacity = INITIAL_CAPACITY; theData = (E[]) new Object[capacity]; } Suppose that instead we use theData = new E[capacity]; Will this work? No, since type of E is not known at compile time, so must create general Object and downcast

23 Chapter 4: Lists and the Collection Interface22 Add(E anEntry) Method The algorithm… Insert new item at position indicated by size Increment the value of size Return true to indicate success The picture…

24 Chapter 4: Lists and the Collection Interface23 Add(E anEntry) Method The code… public boolean add(E anEntry) { if (size == capacity) { reallocate(); } theData[size] = anEntry; size++; return true; } Would this work? theData[size++] = anEntry;

25 Chapter 4: Lists and the Collection Interface24 Add(int index, E anEntry) Method The algorithm… Make room by shifting data elements “up” Insert anEntry at position index The picture…

26 Chapter 4: Lists and the Collection Interface25 Add(int index, E anEntry) Method The code… public void add(int index, E anEntry) { if (index size) { throw new ArrayIndexOutOfBoundsException(index); } if (size == capacity) { reallocate(); } for (int i = size; i > index; i--) { theData[i] = theData[i - 1]; } theData[index] = anEntry; size++; }

27 Chapter 4: Lists and the Collection Interface26 set Method The algorithm and picture are fairly obvious… The code… public E set(int index, E newValue) { if (index = size) { throw new ArrayIndexOutOfBoundsException(index); } E oldValue = theData[index]; theData[index] = newValue; return oldValue; }

28 Chapter 4: Lists and the Collection Interface27 get Method The algorithm and picture are even more obvious… The code… public E get(int index) { if (index = size) { throw new ArrayIndexOutOfBoundsException(index); } return theData[index]; }

29 Chapter 4: Lists and the Collection Interface28 remove Method The algorithm… Remove element Shift “down” to fill in the gap The picture…

30 Chapter 4: Lists and the Collection Interface29 remove Method The code… public E remove(int index) { if (index = size) { throw new ArrayIndexOutOfBoundsException(index); } E returnValue = theData[index]; for (int i = index + 1; i < size; i++) { theData[i - 1] = theData[i]; } size--; return returnValue; }

31 Chapter 4: Lists and the Collection Interface30 reallocate Method The algorithm… Double the size of the underlying array The picture… Not necessary! The code… private void reallocate() { capacity = 2 * capacity; E[] newData = (E[]) new Object[capacity]; System.arraycopy(theData, 0, newData, 0, size); theData = newData; }

32 Chapter 4: Lists and the Collection Interface31 KWArrayList as Collection of Objects Suppose we don’t want a generic collection… public class KWArrayList { // Data Fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The current size */ private int size = 0;..... And each reference to type E[] is now Object[] /** The underlying data array */ private Object[] theData;

33 Chapter 4: Lists and the Collection Interface32 Performance of KWArrayList Set and get methods execute in constant time No loops, so O(1) Inserting or removing elements is linear time Shift (at most) n elements, so O(n) What about reallocate?

34 Chapter 4: Lists and the Collection Interface33 Vector Class Initial release of Java API contained the Vector class which has similar functionality to the ArrayList Both contain the same methods New applications should use ArrayList rather than Vector The Stack class is a subclass of Vector But Stack class is actually useful (next chapter…)

35 Chapter 4: Lists and the Collection Interface34 ArrayList Limitation Suppose we have a (virtual) line of 4 students Give each student a number, 0 thru 3 Where number represents position in line For example: Bob (0), Alice (1), Eve (2), Charlie (3) Suppose Alice leaves Then Eve and Charlie must be “renumbered” In general, this is O(n) operation This is what happens with ArrayList remove Behind the scenes, of course Is there a better way?

36 Chapter 4: Lists and the Collection Interface35 Linked Lists Again, suppose we have a (virtual) line of 4 students Instead of numbering… Tell each student who is behind them For example: Bob  Alice  Eve  Charlie Bob knows Alice is behind him, etc. Now suppose Alice leaves the line Only Bob needs to make a change Bob  Eve  Charlie How to implement this linked list data structure?

37 Chapter 4: Lists and the Collection Interface36 Linked Lists The ArrayList add and remove methods operate in linear time because loop shift elements in an array Linked list overcomes this by providing ability to add or remove items anywhere in the list in constant time Each element (node) in a linked list stores information and a link to the next node This is known as a single-linked list In a double-linked list, each node has a link to the next node and a link to the previous node

38 Chapter 4: Lists and the Collection Interface37 List Node A node contains a data item and one or more links A link is a reference to a node A node class generally defined inside another class Inner class: node details should be private Recall, private is visible to parent class

39 Chapter 4: Lists and the Collection Interface38 Linked List: Insert Suppose we want to insert “Bob” after “Harry” First, set next of Bob to next of Harry Second, change Harry’s next to point to Bob Note that the order matters!

40 Chapter 4: Lists and the Collection Interface39 Linked List: Remove Suppose we want to remove “Dick” from the list First, note that Tom comes before Dick in list Second, change next of Tom to next of Dick Note that we must know predecessor of Dick

41 Chapter 4: Lists and the Collection Interface40 Traversing a Linked List A fairly simple process: 1. Set nodeRef to reference of first node (head) 2. while nodeRef is not null 3. do something with node referenced by nodeRef 4. Set nodeRef to nodeRef.next At end of list, nodeRef = nodeRef.next; Gives null, which is just fine If you then execute this statement again, what happens? The dreaded NullPointerException You have fallen off the end of the linked list!

42 Chapter 4: Lists and the Collection Interface41 Circular Linked List No beginning and no end Kinda like trying to graduate from SJSU… How to create a circular linked list? Set next of last element to point to first element Advantage(s)? Cannot fall off the end of the list Disadvantage(s)? Possible infinite loop

43 Chapter 4: Lists and the Collection Interface42 Double-Linked Lists Limitations of a single-linked list include: Insertion at the front of the list is O(1). Insertion at other positions is O(n) where n is the size of the list. Can insert a node only after a referenced node Can remove a node only if we have a reference to its predecessor node Can traverse the list only in the forward direction These limitations are eliminated by adding a reference in each node to the previous node: double-linked list

44 Chapter 4: Lists and the Collection Interface43 Double-Linked List: Example

45 Chapter 4: Lists and the Collection Interface44 Double-Linked List: Insert We want to insert Sharon between Harry and Sam Set Sharon’s next to Sam Set Sharon’s prev to Harry Set next of Harry to Sharon Set prev of Sam to Sharon

46 Chapter 4: Lists and the Collection Interface45 Double-Linked List: Insert

47 Chapter 4: Lists and the Collection Interface46 Double-Linked List: Insert

48 Chapter 4: Lists and the Collection Interface47 Double-Linked List: Insert (Again) We want to insert Sharon into list before Sam… …only using references to Sam Set sharon.next to sam Set sharon.prev to sam.prev Set sam.prev.next to sharon Set sam.prev to sharon Note that the order matters! Must use sam.prev before changing it

49 Chapter 4: Lists and the Collection Interface48 Double-Linked List: Insert (Again)

50 Chapter 4: Lists and the Collection Interface49 Double-Linked List: Insert (Again)

51 Chapter 4: Lists and the Collection Interface50 Double-Linked List: Remove Suppose we want to remove Harry Where Harry is after Dick and before Sharon Set dick.next to sharon Set sharon.prev to dick

52 Chapter 4: Lists and the Collection Interface51 Double-Linked List: Remove (Again) Suppose we want to remove Harry… …using only references to Harry Set harry.prev.next to harry.next Set harry.next.prev to harry.prev

53 Chapter 4: Lists and the Collection Interface52 Circular Lists Circular Single-Linked List Link last node to the first node That is, tail.next = head; Circular Double-Linked List Link last node to the first node and the first to the last I.e., head.prev = tail; and tail.next = head; Advantage: can traverse in forward or reverse direction even after you have passed the last or first node Can visit all the list elements from any starting point Can never fall off the end of a list Disadvantage: infinite loop!

54 Chapter 4: Lists and the Collection Interface53 Circular Double-Linked List

55 Chapter 4: Lists and the Collection Interface54 LinkedList Class Part of Java API Package java.util Implements the List interface using a double-linked list Contains many of same methods as ArrayList Also some methods not in ArrayList

56 Chapter 4: Lists and the Collection Interface55 LinkedList Class

57 Chapter 4: Lists and the Collection Interface56 Accessing LinkedList Elements How to access each element in a LinkedList? This will work: for (int index = 0; index < aList.size(); index++) { E nextElement = aList.get(index);... } Work appears to be linear, or O(n) But, internally, what does get do? It starts at 0 and goes to index Then actual work is quadratic, or O(n 2 ) Can we do this more efficiently?

58 Chapter 4: Lists and the Collection Interface57 Iterator Interface Iterator defined in package java.util List interface declares the method iterator Returns an Iterator object Iterates over the elements of the list Note that iterator points “between” nodes Use Iterator’s next method to retrieve next element Iterator similar to StringTokenizer object

59 Chapter 4: Lists and the Collection Interface58 Iterator Interface Suppose iter declared an Iterator for myList Where myList is a LinkedList Then can access each element using while (iter.hasNext()) { E nextElement = iter.next();... } How much work is this? Appears to be O(n)…and it really is! How to declare an Iterator object?

60 Chapter 4: Lists and the Collection Interface59 Iterator Interface Here, we process elements of List aList using the Iterator object itr Iterator itr = aList.iterator(); while (itr.hasNext()) { int value = itr.next();... } Again, Iterator object does not point to an element Instead, points “between” elements

61 Chapter 4: Lists and the Collection Interface60 Iterator Interface

62 Chapter 4: Lists and the Collection Interface61 Iterator and remove Method Each call to remove must be preceded by next Use Iterator.remove instead of List.remove For example, to remove all elements divisible by div… public static void remDiv(LinkedList aList, int div) { Iterator iter = aList.iterator(); while (iter.hasNext()) { int nextInt = iter.next(); if (nextInt % div == 0) iter.remove(); }

63 Chapter 4: Lists and the Collection Interface62 ListIterator Interface Iterator limitations Can only traverse the List in the forward direction Provides remove method, but no add method Must manually advance Iterator to start of list ListIterator extends Iterator interface And it overcomes above limitations As with Iterator, a ListIterator lives “between” elements of the linked list

64 Chapter 4: Lists and the Collection Interface63 ListIterator Interface

65 Chapter 4: Lists and the Collection Interface64 ListIterator Interface Create ListIterator starting between elements 2 and 3 List Iterator myIter = myList.listIterator(3); Then myIter.next() returns ref to string at position 3 What do these do? myIter.nextIndex() and myIter.previousIndex() myIter.previous() myIter.hasNext() and myIter.hasPrevious()

66 Chapter 4: Lists and the Collection Interface65 ListIterator Interface To search for first occurrence of target and replace with newItem could use the following… ListIterator myIter = myList.listIterator(); while (myIter.hasNext()) { if (target.equals(myIter.next())) myIter.set(newItem); break; } Can we use Iterator instead?

67 Chapter 4: Lists and the Collection Interface66 Comparison of Iterator and ListIterator ListIterator is a subinterface of Iterator So, classes that implement ListIterator have both Iterator interface requires fewer methods and can be used to iterate over more general data structures But only in one direction Iterator is required by the Collection interface ListIterator is required only by the List interface

68 Chapter 4: Lists and the Collection Interface67 ListIterator and Index ListIterator has the methods nextIndex and previousIndex Return index value of next or previous LinkedList has method listIterator(int index) Returns ListIterator that gives next However, this ListIterator starts at beginning In general, not as efficient as “real” ListIterator

69 Chapter 4: Lists and the Collection Interface68 Enhanced for Statement Aka “for each” statement Makes it easier to use iterators Creates Iterator, implicitly calls hasNext and next Other Iterator methods (remove) not available For example, sum = 0; for (int nextInt: aList) { sum += nextInt; } Can also use “for each” with arrays

70 Chapter 4: Lists and the Collection Interface69 The Enhanced for Statement

71 Chapter 4: Lists and the Collection Interface70 Iterable Interface Requires only that a class that implements it provide an iterator method The Collection interface extends the Iterable interface, so all classes that implement the List interface must provide an iterator method

72 Chapter 4: Lists and the Collection Interface71 Implementation of a Double-Linked List Call it KWLinkedList Why “KW”? A “homebrew” double-linked list and iterator Not a complete implementation For illustration purposes only You should not use KWLinkedList in real applications Recall KWArrayList Illustrated underlying role of arrays in ArrayList Somewhat similar goal here…

73 Chapter 4: Lists and the Collection Interface72 KWLinkedList Data Fields import java.util.*; /** KWLinkedList implements a double-linked list */ public class KWLinkedList { // Data Fields /** A reference to the head of the list. */ private Node head = null; /** A reference to the end of the list. */ private Node tail = null; /** The size of the list. */ private int size = 0;

74 Chapter 4: Lists and the Collection Interface73 KWLinkedList Data Fields

75 Chapter 4: Lists and the Collection Interface74 KWLinkedList Methods add(int index, E obj) get(int index) listIterator(int index)

76 Chapter 4: Lists and the Collection Interface75 KWLinkedList Methods add(int index, E obj) Obtain reference to node at position index Insert new node containting obj before position index get(int index) Obtain reference to node at position index Return contents of node at position index listIterator(int index) Obtain reference to node at position index Create ListIterator positioned just before index

77 Chapter 4: Lists and the Collection Interface76 KWLinkedList Methods All 3 methods have same first step So use one method in each case Also, ListIterator has an add method Inserts new item before current position So, we will use ListIterator to implement add… add(int index, E obj) Obtain iterator positioned just before position index Insert new node containing obj before iterator

78 Chapter 4: Lists and the Collection Interface77 KWLinkedList add Method /** Add an item at the specified index. */ public void add(int index, E obj) { listIterator(index).add(obj); } Note it’s not necessary to declare local ListIterator Use an anonymous ListIterator object

79 Chapter 4: Lists and the Collection Interface78 KWLinkedList get Method /** Get the element at position index. */ public E get(int index) { return listIterator(index).next(); } Other methods: addFirst, addLast, getFirst, getLast These can be implemented by delegation to add and get methods, above

80 Chapter 4: Lists and the Collection Interface79 KWListIter Class Class KWListIter implement the ListIterator interface An inner class of KWLinkedList Can access data fields and members of parent class Data fields… /** Inner class to implement ListIterator interface. */ private class KWListIter implements ListIterator { /** A reference to the next item. */ private Node nextItem; /** A reference to the last item returned. */ private Node lastItemReturned; /** The index of the current item. */ private int index = 0;

81 Chapter 4: Lists and the Collection Interface80 KWListIter Data Fields

82 Chapter 4: Lists and the Collection Interface81 KWListIter Constructor public KWListIter(int i) { // Validate i parameter. if (i size) { throw new IndexOutOfBoundsException( "Invalid index " + i); } lastItemReturned = null; // No item returned yet. // Special case of last item. if (i == size) { index = size; nextItem = null; } else { // Start at the beginning nextItem = head; for (index = 0; index < i; index++) { nextItem = nextItem.next; }

83 Chapter 4: Lists and the Collection Interface82 KWListIter hasNext Method /** Indicate whether movement forward is defined. @return true if call to next will not throw an exception */ public boolean hasNext() { return nextItem != null; }

84 Chapter 4: Lists and the Collection Interface83 KWListIter next Method /** Move the iterator forward and return the next item. @return The next item in the list @throws NoSuchElementException if there is no such object */ public E next() { if (!hasNext()) { throw new NoSuchElementException(); } lastItemReturned = nextItem; nextItem = nextItem.next; index++; return lastItemReturned.data; }

85 Chapter 4: Lists and the Collection Interface84 KWLinkedList and KWListIter Example

86 Chapter 4: Lists and the Collection Interface85 Advancing KWListIter

87 Chapter 4: Lists and the Collection Interface86 KWListIter hasPrevious Method /** Indicate whether movement backward is defined. @return true if call to previous will not throw an exception */ public boolean hasPrevious() { return (nextItem == null && size != 0) || nextItem.prev != null; }

88 Chapter 4: Lists and the Collection Interface87 KWListIter previous Method public E previous() { if (!hasPrevious()) { throw new NoSuchElementException(); } if (nextItem == null) { // past last nextItem = tail; } else { nextItem = nextItem.prev; } lastItemReturned = nextItem; index--; return lastItemReturned.data; }

89 Chapter 4: Lists and the Collection Interface88 KWListIter add Method Inserts new node before nextItem Four cases to consider… Add to an empty list Then head is null Add to head of list Then nextItem is head Add to tail of list Then nextItem is null Add to middle of list All other cases

90 Chapter 4: Lists and the Collection Interface89 add to Empty List public void add(E obj) { if (head == null) { // Add to empty list. head = new Node (obj); tail = head; }

91 Chapter 4: Lists and the Collection Interface90 add to Empty List

92 Chapter 4: Lists and the Collection Interface91 add to Head of List else if (nextItem == head) { // Insert at head // Create a new node. Node newNode = new Node (obj); // Link it to the nextItem. newNode.next = nextItem; // Step 1 // Link nextItem to the new node. nextItem.prev = newNode; // Step 2 // The new node is now the head. head = newNode; // Step 3 }

93 Chapter 4: Lists and the Collection Interface92 add to Head of List

94 Chapter 4: Lists and the Collection Interface93 add to Tail of List else if (nextItem == null) {// Insert tail // Create a new node Node newNode = new Node (obj); // Link the tail to the new node. tail.next = newNode; // Step 1 // Link the new node to the tail. newNode.prev = tail; // Step 2 // The new node is the new tail. tail = newNode; // Step 3 }

95 Chapter 4: Lists and the Collection Interface94 add to Tail of List

96 Chapter 4: Lists and the Collection Interface95 add to Middle of List else { // Insert into the middle. // Create a new node. Node newNode = new Node (obj); // Link it to nextItem.prev. newNode.prev = nextItem.prev; // Step 1 nextItem.prev.next = newNode; // Step 2 // Link it to the nextItem. newNode.next = nextItem; // Step 3 nextItem.prev = newNode; // Step 4 }

97 Chapter 4: Lists and the Collection Interface96 add to Middle of List

98 Chapter 4: Lists and the Collection Interface97 add … The Last Word // Increase size, index, set lastItemReturned size++; index++; lastItemReturned = null; } // End of method add.

99 Chapter 4: Lists and the Collection Interface98 Inner Classes Two inner classes in KWLinkedList That is, Node and KWListIter Declare Node to be static No need for its methods to access data fields of parent class Cannot declare KWListIter to be static Its methods access and modify fields of KWLinkedList Type parameter is considered previously defined So, cannot appear as part of class name

100 Chapter 4: Lists and the Collection Interface99 Application of the LinkedList Class Case study that uses the Java LinkedList class to solve a common problem: maintaining an ordered list

101 Chapter 4: Lists and the Collection Interface100 Application of the LinkedList Class (continued)

102 Chapter 4: Lists and the Collection Interface101 Application of the LinkedList Class (continued)

103 Chapter 4: Lists and the Collection Interface102 The Collection Hierarchy Both the ArrayList and LinkedList represent a collection of objects that can be referenced by means of an index The Collection interface specifies a subset of the methods specified in the List interface

104 Chapter 4: Lists and the Collection Interface103 The Collection Hierarchy (continued)

105 Chapter 4: Lists and the Collection Interface104 Common Features of Collections Collection interface specifies a set of common methods Fundamental features include: Collections grow as needed Collections hold references to objects Collections have at least two constructors

106 Chapter 4: Lists and the Collection Interface105 Chapter Review The List is a generalization of the array The Java API provides the ArrayList class, which uses an array as the underlying structure to implement the List A linked list consists of a set of nodes, each of which contains its data and a reference to the next node To find an item at a position indicated by an index in a linked list requires traversing the list from the beginning until the item at the specified index is found An iterator gives with the ability to access the items in a List sequentially

107 Chapter 4: Lists and the Collection Interface106 Chapter Review (continued) The ListIterator interface is an extension of the Iterator interface The Java API provides the LinkedList class, which uses a double-linked list to implement the List interface The Iterable interface is the root of the Collection hierarchy The Collection interface and the List interface define a large number of methods that make these abstractions useful for many applications


Download ppt "Lists and the Collection Interface Chapter 4 Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface."

Similar presentations


Ads by Google