📄 abstractlist.java
字号:
/* * @(#)AbstractList.java 1.37 03/01/18 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.util;/** * This class provides a skeletal implementation of the <tt>List</tt> * interface to minimize the effort required to implement this interface * backed by a "random access" data store (such as an array). For sequential * access data (such as a linked list), <tt>AbstractSequentialList</tt> should * be used in preference to this class.<p> * * To implement an unmodifiable list, the programmer needs only to extend this * class and provide implementations for the <tt>get(int index)</tt> and * <tt>size()</tt> methods.<p> * * To implement a modifiable list, the programmer must additionally override * the <tt>set(int index, Object element)</tt> method (which otherwise throws * an <tt>UnsupportedOperationException</tt>. If the list is variable-size * the programmer must additionally override the <tt>add(int index, Object * element)</tt> and <tt>remove(int index)</tt> methods.<p> * * The programmer should generally provide a void (no argument) and collection * constructor, as per the recommendation in the <tt>Collection</tt> interface * specification.<p> * * Unlike the other abstract collection implementations, the programmer does * <i>not</i> have to provide an iterator implementation; the iterator and * list iterator are implemented by this class, on top the "random access" * methods: <tt>get(int index)</tt>, <tt>set(int index, Object element)</tt>, * <tt>set(int index, Object element)</tt>, <tt>add(int index, Object * element)</tt> and <tt>remove(int index)</tt>.<p> * * The documentation for each non-abstract methods in this class describes its * implementation in detail. Each of these methods may be overridden if the * collection being implemented admits a more efficient implementation.<p> * * This class is a member of the * <a href="{@docRoot}/../guide/collections/index.html"> * Java Collections Framework</a>. * * @author Josh Bloch * @version 1.37, 01/18/03 * @see Collection * @see List * @see AbstractSequentialList * @see AbstractCollection * @since 1.2 */public abstract class AbstractList extends AbstractCollection implements List { /** * Sole constructor. (For invocation by subclass constructors, typically * implicit.) */ protected AbstractList() { } /** * Appends the specified element to the end of this List (optional * operation). <p> * * This implementation calls <tt>add(size(), o)</tt>.<p> * * Note that this implementation throws an * <tt>UnsupportedOperationException</tt> unless <tt>add(int, Object)</tt> * is overridden. * * @param o element to be appended to this list. * * @return <tt>true</tt> (as per the general contract of * <tt>Collection.add</tt>). * * @throws UnsupportedOperationException if the <tt>add</tt> method is not * supported by this Set. * * @throws ClassCastException if the class of the specified element * prevents it from being added to this set. * * @throws IllegalArgumentException some aspect of this element prevents * it from being added to this collection. */ public boolean add(Object o) { add(size(), o); return true; } /** * Returns the element at the specified position in this list. * * @param index index of element to return. * * @return the element at the specified position in this list. * @throws IndexOutOfBoundsException if the given index is out of range * (<tt>index < 0 || index >= size()</tt>). */ abstract public Object get(int index); /** * Replaces the element at the specified position in this list with the * specified element (optional operation). <p> * * This implementation always throws an * <tt>UnsupportedOperationException</tt>. * * @param index index of element to replace. * @param element element to be stored at the specified position. * @return the element previously at the specified position. * * @throws UnsupportedOperationException if the <tt>set</tt> method is not * supported by this List. * @throws ClassCastException if the class of the specified element * prevents it from being added to this list. * @throws IllegalArgumentException if some aspect of the specified * element prevents it from being added to this list. * * @throws IndexOutOfBoundsException if the specified index is out of * range (<tt>index < 0 || index >= size()</tt>). */ public Object set(int index, Object element) { throw new UnsupportedOperationException(); } /** * Inserts the specified element at the specified position in this list * (optional operation). Shifts the element currently at that position * (if any) and any subsequent elements to the right (adds one to their * indices).<p> * * This implementation always throws an UnsupportedOperationException. * * @param index index at which the specified element is to be inserted. * @param element element to be inserted. * * @throws UnsupportedOperationException if the <tt>add</tt> method is not * supported by this list. * @throws ClassCastException if the class of the specified element * prevents it from being added to this list. * @throws IllegalArgumentException if some aspect of the specified * element prevents it from being added to this list. * @throws IndexOutOfBoundsException index is out of range (<tt>index < * 0 || index > size()</tt>). */ public void add(int index, Object element) { throw new UnsupportedOperationException(); } /** * Removes the element at the specified position in this list (optional * operation). Shifts any subsequent elements to the left (subtracts one * from their indices). Returns the element that was removed from the * list.<p> * * This implementation always throws an * <tt>UnsupportedOperationException</tt>. * * @param index the index of the element to remove. * @return the element previously at the specified position. * * @throws UnsupportedOperationException if the <tt>remove</tt> method is * not supported by this list. * @throws IndexOutOfBoundsException if the specified index is out of * range (<tt>index < 0 || index >= size()</tt>). */ public Object remove(int index) { throw new UnsupportedOperationException(); } // Search Operations /** * Returns the index in this list of the first occurence of the specified * element, or -1 if the list does not contain this element. More * formally, returns the lowest index <tt>i</tt> such that <tt>(o==null ? * get(i)==null : o.equals(get(i)))</tt>, or -1 if there is no such * index.<p> * * This implementation first gets a list iterator (with * <tt>listIterator()</tt>). Then, it iterates over the list until the * specified element is found or the end of the list is reached. * * @param o element to search for. * * @return the index in this List of the first occurence of the specified * element, or -1 if the List does not contain this element. */ public int indexOf(Object o) { ListIterator e = listIterator(); if (o==null) { while (e.hasNext()) if (e.next()==null) return e.previousIndex(); } else { while (e.hasNext()) if (o.equals(e.next())) return e.previousIndex(); } return -1; } /** * Returns the index in this list of the last occurence of the specified * element, or -1 if the list does not contain this element. More * formally, returns the highest index <tt>i</tt> such that <tt>(o==null ? * get(i)==null : o.equals(get(i)))</tt>, or -1 if there is no such * index.<p> * * This implementation first gets a list iterator that points to the end * of the list (with listIterator(size())). Then, it iterates backwards * over the list until the specified element is found, or the beginning of * the list is reached. * * @param o element to search for. * * @return the index in this list of the last occurence of the specified * element, or -1 if the list does not contain this element. */ public int lastIndexOf(Object o) { ListIterator e = listIterator(size()); if (o==null) { while (e.hasPrevious()) if (e.previous()==null) return e.nextIndex(); } else { while (e.hasPrevious()) if (o.equals(e.previous())) return e.nextIndex(); } return -1; } // Bulk Operations /** * Removes all of the elements from this collection (optional operation). * The collection will be empty after this call returns (unless it throws * an exception).<p> * * This implementation calls <tt>removeRange(0, size())</tt>.<p> * * Note that this implementation throws an * <tt>UnsupportedOperationException</tt> unless <tt>remove(int * index)</tt> or <tt>removeRange(int fromIndex, int toIndex)</tt> is * overridden. * * @throws UnsupportedOperationException if the <tt>clear</tt> method is * not supported by this Collection. */ public void clear() { removeRange(0, size()); } /** * Inserts all of the elements in the specified collection into this list * at the specified position (optional operation). Shifts the element * currently at that position (if any) and any subsequent elements to the * right (increases their indices). The new elements will appear in the * list in the order that they are returned by the specified collection's * iterator. The behavior of this operation is unspecified if the * specified collection is modified while the operation is in progress. * (Note that this will occur if the specified collection is this list, * and it's nonempty.)<p> * * This implementation gets an iterator over the specified collection and * iterates over it, inserting the elements obtained from the iterator * into this list at the appropriate position, one at a time, using * <tt>add(int, Object)</tt>. Many implementations will override this * method for efficiency.<p> * * Note that this implementation throws an * <tt>UnsupportedOperationException</tt> unless <tt>add(int, Object)</tt> * is overridden. * * @return <tt>true</tt> if this list changed as a result of the call. * @param index index at which to insert the first element from the * specified collection. * @param c elements to be inserted into this List. * * @throws UnsupportedOperationException if the <tt>addAll</tt> method is * not supported by this list. * * @throws ClassCastException if the class of an element of the specified
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -