8 |
|
package java.util; |
9 |
|
|
10 |
|
/** |
11 |
< |
* This class provides a skeletal implementation of the <tt>List</tt> |
11 |
> |
* This class provides a skeletal implementation of the {@code List} |
12 |
|
* interface to minimize the effort required to implement this interface |
13 |
|
* backed by a "random access" data store (such as an array). For sequential |
14 |
< |
* access data (such as a linked list), <tt>AbstractSequentialList</tt> should |
15 |
< |
* be used in preference to this class.<p> |
14 |
> |
* access data (such as a linked list), {@code AbstractSequentialList} should |
15 |
> |
* be used in preference to this class. |
16 |
|
* |
17 |
< |
* To implement an unmodifiable list, the programmer needs only to extend this |
18 |
< |
* class and provide implementations for the <tt>get(int index)</tt> and |
19 |
< |
* <tt>size()</tt> methods.<p> |
17 |
> |
* <p>To implement an unmodifiable list, the programmer needs only to extend this |
18 |
> |
* class and provide implementations for the {@code get(int index)} and |
19 |
> |
* {@code size()} methods. |
20 |
|
* |
21 |
< |
* To implement a modifiable list, the programmer must additionally override |
22 |
< |
* the <tt>set(int index, Object element)</tt> method (which otherwise throws |
23 |
< |
* an <tt>UnsupportedOperationException</tt>. If the list is variable-size |
24 |
< |
* the programmer must additionally override the <tt>add(int index, Object |
25 |
< |
* element)</tt> and <tt>remove(int index)</tt> methods.<p> |
21 |
> |
* <p>To implement a modifiable list, the programmer must additionally override |
22 |
> |
* the {@code set(int index, Object element)} method (which otherwise throws |
23 |
> |
* an {@code UnsupportedOperationException}. If the list is variable-size |
24 |
> |
* the programmer must additionally override the {@code add(int index, Object |
25 |
> |
* element)} and {@code remove(int index)} methods. |
26 |
|
* |
27 |
< |
* The programmer should generally provide a void (no argument) and collection |
28 |
< |
* constructor, as per the recommendation in the <tt>Collection</tt> interface |
29 |
< |
* specification.<p> |
27 |
> |
* <p>The programmer should generally provide a void (no argument) and collection |
28 |
> |
* constructor, as per the recommendation in the {@code Collection} interface |
29 |
> |
* specification. |
30 |
|
* |
31 |
< |
* Unlike the other abstract collection implementations, the programmer does |
31 |
> |
* <p>Unlike the other abstract collection implementations, the programmer does |
32 |
|
* <i>not</i> have to provide an iterator implementation; the iterator and |
33 |
|
* list iterator are implemented by this class, on top of the "random access" |
34 |
< |
* methods: <tt>get(int index)</tt>, <tt>set(int index, E element)</tt>, |
35 |
< |
* <tt>add(int index, E element)</tt> and <tt>remove(int index)</tt>.<p> |
34 |
> |
* methods: {@code get(int index)}, {@code set(int index, E element)}, |
35 |
> |
* {@code add(int index, E element)} and {@code remove(int index)}. |
36 |
|
* |
37 |
< |
* The documentation for each non-abstract methods in this class describes its |
37 |
> |
* <p>The documentation for each non-abstract methods in this class describes its |
38 |
|
* implementation in detail. Each of these methods may be overridden if the |
39 |
< |
* collection being implemented admits a more efficient implementation.<p> |
39 |
> |
* collection being implemented admits a more efficient implementation. |
40 |
|
* |
41 |
< |
* This class is a member of the |
41 |
> |
* <p>This class is a member of the |
42 |
|
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
43 |
|
* Java Collections Framework</a>. |
44 |
|
* |
71 |
|
* classes should clearly specify in their documentation any restrictions |
72 |
|
* on what elements may be added. |
73 |
|
* |
74 |
< |
* <p>This implementation calls <tt>add(size(), e)</tt>. |
74 |
> |
* <p>This implementation calls {@code add(size(), e)}. |
75 |
|
* |
76 |
|
* <p>Note that this implementation throws an |
77 |
< |
* <tt>UnsupportedOperationException</tt> unless <tt>add(int, Object)</tt> |
77 |
> |
* {@code UnsupportedOperationException} unless {@code add(int, Object)} |
78 |
|
* is overridden. |
79 |
|
* |
80 |
|
* @param e element to be appended to this list |
81 |
< |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
82 |
< |
* @throws UnsupportedOperationException if the <tt>add</tt> operation |
81 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
82 |
> |
* @throws UnsupportedOperationException if the {@code add} operation |
83 |
|
* is not supported by this list |
84 |
|
* @throws ClassCastException if the class of the specified element |
85 |
|
* prevents it from being added to this list |
104 |
|
* {@inheritDoc} |
105 |
|
* |
106 |
|
* <p>This implementation always throws an |
107 |
< |
* <tt>UnsupportedOperationException</tt>. |
107 |
> |
* {@code UnsupportedOperationException}. |
108 |
|
* |
109 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
110 |
|
* @throws ClassCastException {@inheritDoc} |
120 |
|
* {@inheritDoc} |
121 |
|
* |
122 |
|
* <p>This implementation always throws an |
123 |
< |
* <tt>UnsupportedOperationException</tt>. |
123 |
> |
* {@code UnsupportedOperationException}. |
124 |
|
* |
125 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
126 |
|
* @throws ClassCastException {@inheritDoc} |
136 |
|
* {@inheritDoc} |
137 |
|
* |
138 |
|
* <p>This implementation always throws an |
139 |
< |
* <tt>UnsupportedOperationException</tt>. |
139 |
> |
* {@code UnsupportedOperationException}. |
140 |
|
* |
141 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
142 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
152 |
|
* {@inheritDoc} |
153 |
|
* |
154 |
|
* <p>This implementation first gets a list iterator (with |
155 |
< |
* <tt>listIterator()</tt>). Then, it iterates over the list until the |
155 |
> |
* {@code listIterator()}). Then, it iterates over the list until the |
156 |
|
* specified element is found or the end of the list is reached. |
157 |
|
* |
158 |
|
* @throws ClassCastException {@inheritDoc} |
176 |
|
* {@inheritDoc} |
177 |
|
* |
178 |
|
* <p>This implementation first gets a list iterator that points to the end |
179 |
< |
* of the list (with <tt>listIterator(size())</tt>). Then, it iterates |
179 |
> |
* of the list (with {@code listIterator(size())}). Then, it iterates |
180 |
|
* backwards over the list until the specified element is found, or the |
181 |
|
* beginning of the list is reached. |
182 |
|
* |
204 |
|
* Removes all of the elements from this list (optional operation). |
205 |
|
* The list will be empty after this call returns. |
206 |
|
* |
207 |
< |
* <p>This implementation calls <tt>removeRange(0, size())</tt>. |
207 |
> |
* <p>This implementation calls {@code removeRange(0, size())}. |
208 |
|
* |
209 |
|
* <p>Note that this implementation throws an |
210 |
< |
* <tt>UnsupportedOperationException</tt> unless <tt>remove(int |
211 |
< |
* index)</tt> or <tt>removeRange(int fromIndex, int toIndex)</tt> is |
210 |
> |
* {@code UnsupportedOperationException} unless {@code remove(int |
211 |
> |
* index)} or {@code removeRange(int fromIndex, int toIndex)} is |
212 |
|
* overridden. |
213 |
|
* |
214 |
< |
* @throws UnsupportedOperationException if the <tt>clear</tt> operation |
214 |
> |
* @throws UnsupportedOperationException if the {@code clear} operation |
215 |
|
* is not supported by this list |
216 |
|
*/ |
217 |
|
public void clear() { |
224 |
|
* <p>This implementation gets an iterator over the specified collection and |
225 |
|
* iterates over it, inserting the elements obtained from the iterator |
226 |
|
* into this list at the appropriate position, one at a time, using |
227 |
< |
* <tt>add(int, Object)</tt>. Many implementations will override this |
227 |
> |
* {@code add(int, Object)}. Many implementations will override this |
228 |
|
* method for efficiency. |
229 |
|
* |
230 |
|
* <p>Note that this implementation throws an |
231 |
< |
* <tt>UnsupportedOperationException</tt> unless <tt>add(int, Object)</tt> |
231 |
> |
* {@code UnsupportedOperationException} unless {@code add(int, Object)} |
232 |
|
* is overridden. |
233 |
|
* |
234 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
255 |
|
* sequence. <p> |
256 |
|
* |
257 |
|
* This implementation returns a straightforward implementation of the |
258 |
< |
* iterator interface, relying on the backing list's <tt>size()</tt>, |
259 |
< |
* <tt>get(int)</tt>, and <tt>remove(int)</tt> methods.<p> |
258 |
> |
* iterator interface, relying on the backing list's {@code size()}, |
259 |
> |
* {@code get(int)}, and {@code remove(int)} methods.<p> |
260 |
|
* |
261 |
|
* Note that the iterator returned by this method will throw an |
262 |
< |
* <tt>UnsupportedOperationException</tt> in response to its |
263 |
< |
* <tt>remove</tt> method unless the list's <tt>remove(int)</tt> method is |
262 |
> |
* {@code UnsupportedOperationException} in response to its |
263 |
> |
* {@code remove} method unless the list's {@code remove(int)} method is |
264 |
|
* overridden.<p> |
265 |
|
* |
266 |
|
* This implementation can be made to throw runtime exceptions in the face |
267 |
|
* of concurrent modification, as described in the specification for the |
268 |
< |
* (protected) <tt>modCount</tt> field. |
268 |
> |
* (protected) {@code modCount} field. |
269 |
|
* |
270 |
|
* @return an iterator over the elements in this list in proper sequence |
271 |
|
* |
278 |
|
/** |
279 |
|
* {@inheritDoc} |
280 |
|
* |
281 |
< |
* <p>This implementation returns <tt>listIterator(0)</tt>. |
281 |
> |
* <p>This implementation returns {@code listIterator(0)}. |
282 |
|
* |
283 |
|
* @see #listIterator(int) |
284 |
|
*/ |
290 |
|
* {@inheritDoc} |
291 |
|
* |
292 |
|
* <p>This implementation returns a straightforward implementation of the |
293 |
< |
* <tt>ListIterator</tt> interface that extends the implementation of the |
294 |
< |
* <tt>Iterator</tt> interface returned by the <tt>iterator()</tt> method. |
295 |
< |
* The <tt>ListIterator</tt> implementation relies on the backing list's |
296 |
< |
* <tt>get(int)</tt>, <tt>set(int, Object)</tt>, <tt>add(int, Object)</tt> |
297 |
< |
* and <tt>remove(int)</tt> methods. |
293 |
> |
* {@code ListIterator} interface that extends the implementation of the |
294 |
> |
* {@code Iterator} interface returned by the {@code iterator()} method. |
295 |
> |
* The {@code ListIterator} implementation relies on the backing list's |
296 |
> |
* {@code get(int)}, {@code set(int, Object)}, {@code add(int, Object)} |
297 |
> |
* and {@code remove(int)} methods. |
298 |
|
* |
299 |
|
* <p>Note that the list iterator returned by this implementation will |
300 |
< |
* throw an <tt>UnsupportedOperationException</tt> in response to its |
301 |
< |
* <tt>remove</tt>, <tt>set</tt> and <tt>add</tt> methods unless the |
302 |
< |
* list's <tt>remove(int)</tt>, <tt>set(int, Object)</tt>, and |
303 |
< |
* <tt>add(int, Object)</tt> methods are overridden. |
300 |
> |
* throw an {@code UnsupportedOperationException} in response to its |
301 |
> |
* {@code remove}, {@code set} and {@code add} methods unless the |
302 |
> |
* list's {@code remove(int)}, {@code set(int, Object)}, and |
303 |
> |
* {@code add(int, Object)} methods are overridden. |
304 |
|
* |
305 |
|
* <p>This implementation can be made to throw runtime exceptions in the |
306 |
|
* face of concurrent modification, as described in the specification for |
307 |
< |
* the (protected) <tt>modCount</tt> field. |
307 |
> |
* the (protected) {@code modCount} field. |
308 |
|
* |
309 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
310 |
|
* |
437 |
|
* {@inheritDoc} |
438 |
|
* |
439 |
|
* <p>This implementation returns a list that subclasses |
440 |
< |
* <tt>AbstractList</tt>. The subclass stores, in private fields, the |
440 |
> |
* {@code AbstractList}. The subclass stores, in private fields, the |
441 |
|
* offset of the subList within the backing list, the size of the subList |
442 |
|
* (which can change over its lifetime), and the expected |
443 |
< |
* <tt>modCount</tt> value of the backing list. There are two variants |
444 |
< |
* of the subclass, one of which implements <tt>RandomAccess</tt>. |
445 |
< |
* If this list implements <tt>RandomAccess</tt> the returned list will |
446 |
< |
* be an instance of the subclass that implements <tt>RandomAccess</tt>. |
447 |
< |
* |
448 |
< |
* <p>The subclass's <tt>set(int, Object)</tt>, <tt>get(int)</tt>, |
449 |
< |
* <tt>add(int, Object)</tt>, <tt>remove(int)</tt>, <tt>addAll(int, |
450 |
< |
* Collection)</tt> and <tt>removeRange(int, int)</tt> methods all |
443 |
> |
* {@code modCount} value of the backing list. There are two variants |
444 |
> |
* of the subclass, one of which implements {@code RandomAccess}. |
445 |
> |
* If this list implements {@code RandomAccess} the returned list will |
446 |
> |
* be an instance of the subclass that implements {@code RandomAccess}. |
447 |
> |
* |
448 |
> |
* <p>The subclass's {@code set(int, Object)}, {@code get(int)}, |
449 |
> |
* {@code add(int, Object)}, {@code remove(int)}, {@code addAll(int, |
450 |
> |
* Collection)} and {@code removeRange(int, int)} methods all |
451 |
|
* delegate to the corresponding methods on the backing abstract list, |
452 |
|
* after bounds-checking the index and adjusting for the offset. The |
453 |
< |
* <tt>addAll(Collection c)</tt> method merely returns <tt>addAll(size, |
454 |
< |
* c)</tt>. |
453 |
> |
* {@code addAll(Collection c)} method merely returns {@code addAll(size, |
454 |
> |
* c)}. |
455 |
|
* |
456 |
< |
* <p>The <tt>listIterator(int)</tt> method returns a "wrapper object" |
456 |
> |
* <p>The {@code listIterator(int)} method returns a "wrapper object" |
457 |
|
* over a list iterator on the backing list, which is created with the |
458 |
< |
* corresponding method on the backing list. The <tt>iterator</tt> method |
459 |
< |
* merely returns <tt>listIterator()</tt>, and the <tt>size</tt> method |
460 |
< |
* merely returns the subclass's <tt>size</tt> field. |
458 |
> |
* corresponding method on the backing list. The {@code iterator} method |
459 |
> |
* merely returns {@code listIterator()}, and the {@code size} method |
460 |
> |
* merely returns the subclass's {@code size} field. |
461 |
|
* |
462 |
< |
* <p>All methods first check to see if the actual <tt>modCount</tt> of |
462 |
> |
* <p>All methods first check to see if the actual {@code modCount} of |
463 |
|
* the backing list is equal to its expected value, and throw a |
464 |
< |
* <tt>ConcurrentModificationException</tt> if it is not. |
464 |
> |
* {@code ConcurrentModificationException} if it is not. |
465 |
|
* |
466 |
|
* @throws IndexOutOfBoundsException endpoint index value out of range |
467 |
< |
* <tt>(fromIndex < 0 || toIndex > size)</tt> |
467 |
> |
* {@code (fromIndex < 0 || toIndex > size)} |
468 |
|
* @throws IllegalArgumentException if the endpoint indices are out of order |
469 |
< |
* <tt>(fromIndex > toIndex)</tt> |
469 |
> |
* {@code (fromIndex > toIndex)} |
470 |
|
*/ |
471 |
|
public List<E> subList(int fromIndex, int toIndex) { |
472 |
|
return (this instanceof RandomAccess ? |
478 |
|
|
479 |
|
/** |
480 |
|
* Compares the specified object with this list for equality. Returns |
481 |
< |
* <tt>true</tt> if and only if the specified object is also a list, both |
481 |
> |
* {@code true} if and only if the specified object is also a list, both |
482 |
|
* lists have the same size, and all corresponding pairs of elements in |
483 |
< |
* the two lists are <i>equal</i>. (Two elements <tt>e1</tt> and |
484 |
< |
* <tt>e2</tt> are <i>equal</i> if <tt>(e1==null ? e2==null : |
485 |
< |
* e1.equals(e2))</tt>.) In other words, two lists are defined to be |
483 |
> |
* the two lists are <i>equal</i>. (Two elements {@code e1} and |
484 |
> |
* {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null : |
485 |
> |
* e1.equals(e2))}.) In other words, two lists are defined to be |
486 |
|
* equal if they contain the same elements in the same order.<p> |
487 |
|
* |
488 |
|
* This implementation first checks if the specified object is this |
489 |
< |
* list. If so, it returns <tt>true</tt>; if not, it checks if the |
490 |
< |
* specified object is a list. If not, it returns <tt>false</tt>; if so, |
489 |
> |
* list. If so, it returns {@code true}; if not, it checks if the |
490 |
> |
* specified object is a list. If not, it returns {@code false}; if so, |
491 |
|
* it iterates over both lists, comparing corresponding pairs of elements. |
492 |
< |
* If any comparison returns <tt>false</tt>, this method returns |
493 |
< |
* <tt>false</tt>. If either iterator runs out of elements before the |
494 |
< |
* other it returns <tt>false</tt> (as the lists are of unequal length); |
495 |
< |
* otherwise it returns <tt>true</tt> when the iterations complete. |
492 |
> |
* If any comparison returns {@code false}, this method returns |
493 |
> |
* {@code false}. If either iterator runs out of elements before the |
494 |
> |
* other it returns {@code false} (as the lists are of unequal length); |
495 |
> |
* otherwise it returns {@code true} when the iterations complete. |
496 |
|
* |
497 |
|
* @param o the object to be compared for equality with this list |
498 |
< |
* @return <tt>true</tt> if the specified object is equal to this list |
498 |
> |
* @return {@code true} if the specified object is equal to this list |
499 |
|
*/ |
500 |
|
public boolean equals(Object o) { |
501 |
|
if (o == this) |
535 |
|
|
536 |
|
/** |
537 |
|
* Removes from this list all of the elements whose index is between |
538 |
< |
* <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive. |
538 |
> |
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. |
539 |
|
* Shifts any succeeding elements to the left (reduces their index). |
540 |
< |
* This call shortens the ArrayList by <tt>(toIndex - fromIndex)</tt> |
541 |
< |
* elements. (If <tt>toIndex==fromIndex</tt>, this operation has no |
540 |
> |
* This call shortens the ArrayList by {@code (toIndex - fromIndex)} |
541 |
> |
* elements. (If {@code toIndex==fromIndex}, this operation has no |
542 |
|
* effect.)<p> |
543 |
|
* |
544 |
< |
* This method is called by the <tt>clear</tt> operation on this list |
544 |
> |
* This method is called by the {@code clear} operation on this list |
545 |
|
* and its subLists. Overriding this method to take advantage of |
546 |
|
* the internals of the list implementation can <i>substantially</i> |
547 |
< |
* improve the performance of the <tt>clear</tt> operation on this list |
547 |
> |
* improve the performance of the {@code clear} operation on this list |
548 |
|
* and its subLists.<p> |
549 |
|
* |
550 |
|
* This implementation gets a list iterator positioned before |
551 |
< |
* <tt>fromIndex</tt>, and repeatedly calls <tt>ListIterator.next</tt> |
552 |
< |
* followed by <tt>ListIterator.remove</tt> until the entire range has |
553 |
< |
* been removed. <b>Note: if <tt>ListIterator.remove</tt> requires linear |
551 |
> |
* {@code fromIndex}, and repeatedly calls {@code ListIterator.next} |
552 |
> |
* followed by {@code ListIterator.remove} until the entire range has |
553 |
> |
* been removed. <b>Note: if {@code ListIterator.remove} requires linear |
554 |
|
* time, this implementation requires quadratic time.</b> |
555 |
|
* |
556 |
|
* @param fromIndex index of first element to be removed |
571 |
|
* progress may yield incorrect results.<p> |
572 |
|
* |
573 |
|
* This field is used by the iterator and list iterator implementation |
574 |
< |
* returned by the <tt>iterator</tt> and <tt>listIterator</tt> methods. |
574 |
> |
* returned by the {@code iterator} and {@code listIterator} methods. |
575 |
|
* If the value of this field changes unexpectedly, the iterator (or list |
576 |
< |
* iterator) will throw a <tt>ConcurrentModificationException</tt> in |
577 |
< |
* response to the <tt>next</tt>, <tt>remove</tt>, <tt>previous</tt>, |
578 |
< |
* <tt>set</tt> or <tt>add</tt> operations. This provides |
576 |
> |
* iterator) will throw a {@code ConcurrentModificationException} in |
577 |
> |
* response to the {@code next}, {@code remove}, {@code previous}, |
578 |
> |
* {@code set} or {@code add} operations. This provides |
579 |
|
* <i>fail-fast</i> behavior, rather than non-deterministic behavior in |
580 |
|
* the face of concurrent modification during iteration.<p> |
581 |
|
* |
582 |
|
* <b>Use of this field by subclasses is optional.</b> If a subclass |
583 |
|
* wishes to provide fail-fast iterators (and list iterators), then it |
584 |
< |
* merely has to increment this field in its <tt>add(int, Object)</tt> and |
585 |
< |
* <tt>remove(int)</tt> methods (and any other methods that it overrides |
584 |
> |
* merely has to increment this field in its {@code add(int, Object)} and |
585 |
> |
* {@code remove(int)} methods (and any other methods that it overrides |
586 |
|
* that result in structural modifications to the list). A single call to |
587 |
< |
* <tt>add(int, Object)</tt> or <tt>remove(int)</tt> must add no more than |
587 |
> |
* {@code add(int, Object)} or {@code remove(int)} must add no more than |
588 |
|
* one to this field, or the iterators (and list iterators) will throw |
589 |
< |
* bogus <tt>ConcurrentModificationExceptions</tt>. If an implementation |
589 |
> |
* bogus {@code ConcurrentModificationExceptions}. If an implementation |
590 |
|
* does not wish to provide fail-fast iterators, this field may be |
591 |
|
* ignored. |
592 |
|
*/ |