The linked list iterators have been designed to detect such modifications. For example, suppose an iterator points before an element that another iterator has just removed. t(newValue) // sets first element to newValueĪs you might imagine, if an iterator traverses a collection while another iterator is modifying it, confusing situations can String oldValue = iter.next() // returns first element For example, the following code replaces the first element of a list with a new value: Unlike the add method, which depends only on the iterator position, the remove method depends on the iterator state.įinally, a set method replaces the last element returned by a call to next or previous with a new element. And you can't call remove twice in a row. However, if you just called previous, the element to the right is removed. Immediately after a call to next, the remove method indeed removes the element to the left of the iterator, just like the BACKSPACE key would. The remove operation does not quite work like the BACKSPACE key. You have to be careful with the “cursor” analogy. For example, the following code skips past the first element in the linked list and adds "Juliet" before the second element (see Figure 2-7). The add method adds the new element before the iterator position. ListIterator iter = staff.listIterator() The listIterator method of the LinkedList class returns an iterator object that implements the ListIterator interface. Like the next method, the previous method returns the object that it skipped over. ![]() In addition, the ListIterator interface has two methods that you can use for traversing a list backwards. Unlike Collection.add, this method does not return a boolean-it is assumed that the add operation always modifies the list. Instead, the collections library supplies a subinterface ListIterator that contains an add method: ![]() Therefore, there is no add method in the Iterator interface. For example, the set data type that we discuss in the next section does not impose any ordering on its elements. Makes sense only for collections that have a natural ordering. Position-dependent add method is the responsibility of an iterator, since iterators describe positions in collections. But you often want to add objects somewhere in the middle of a list. The LinkedList.add method adds the object to the end of the list. A linked list is an ordered collection in which the position of the objects matters. There is, however, an important difference between linked lists and generic collections. Iter.remove() // remove last visited element String second = iter.next() // visit second element String first = iter.next() // visit first element List staff = new LinkedList() // LinkedList implements List The following code example adds three elements and and then removes the second one. Java collections library supplies a class LinkedList ready for you to use. If so, you will be pleased to learn that the ![]() Of tangling up the links when removing or adding elements in the linked list. Perhaps you once took a data structures course in which you learned how to implement linked lists. The same is true for inserting elements in the middle.įigure 2-6 Removing an element from a linked list The middle of an array is expensive since all array elements beyond the removed one must be moved toward the beginning of However, arrays and array lists suffer from a major drawback. We used arrays and their dynamic cousin, the ArrayList class, for many examples in Volume 1. Concrete Collections in the Java LibraryĪn indexed sequence that grows and shrinks dynamicallyĪn ordered sequence that allows efficient insertions and removal at any locationĪn unordered collection that rejects duplicatesĪ set that remembers the order in which elements were insertedĪ collection that allows efficient removal of the smallest elementĪ data structure that stores key/value associationsĪ map in which the keys belong to an enumerated typeĪ map that remembers the order in which entries were addedĪ map with values that can be reclaimed by the garbage collector if they are not used elsewhereĪ map with keys that are compared by =, not equals We will discuss the Map interface on page 110. Those classes implement the Map interface instead. Omit the thread-safe collections that were discussed in Chapter 1.) All classes in Table 2-1 implement the Collection interface, with the exception of the classes with names ending in Map. Table 2-1 shows the collections in the Java library and briefly describes the purpose of each collection class. Return to abstract considerations and see how the collections framework organizes these classes. Once we have thoroughly described the classes you might want to use, we will Rather than getting into more details about all the interfaces, we thought it would be helpful to first discuss the concreteĭata structures that the Java library supplies. Core Java 2, Volume II-Advanced Features, 7th Edition
0 Comments
Leave a Reply. |