Java Reference
In-Depth Information
115
return 0 ;
116 }
117
118 @Override /** Replace the element at the specified position
119 * in this list with the specified element. */
120 public E set( int index, E e) {
121 System.out.println( "Implementation left as an exercise" );
122
set
return null ;
123 }
124
125 @Override /** Override iterator() defined in Iterable */
126
public java.util.Iterator<E> iterator() {
iterator
127
return new LinkedListIterator();
128 }
129
130
private class LinkedListIterator
LinkedListIterator class
131
implements java.util.Iterator<E> {
132
private Node<E> current = head; // Current index
133
134 @Override
135
public boolean hasNext() {
136
return (current != null );
137 }
138
139 @Override
140 public E next() {
141 E e = current.element;
142 current = current.next;
143
return e;
144 }
145
146 @Override
147 public void remove() {
148 System.out.println( "Implementation left as an exercise" );
149 }
150 }
151
152 // This class is only used in LinkedList, so it is private.
153 // This class does not need to access any
154 // instance members of LinkedList, so it is defined static.
155 private static class Node<E> {
156 E element;
157 Node<E> next;
158
159
Node inner class
public Node(E element) {
160
this .element = element;
161 }
162 }
163 }
24.4.4 MyArrayList vs. MyLinkedList
Both MyArrayList and MyLinkedList can be used to store a list. MyArrayList is imple-
mented using an array and MyLinkedList is implemented using a linked list. The overhead
of MyArrayList is smaller than that of MyLinkedList . However, MyLinkedList is more
efficient if you need to insert elements into and delete elements from the beginning of the list.
TableĀ 24.1 summarizes the complexity of the methods in MyArrayList and MyLinkedList .
Note that MyArrayList is the same as java.util.ArrayList and MyLinkedList is the
same as java.util.LinkedList .
 
 
Search WWH ::




Custom Search