//List 接口
public interface List<AnyType> extends Collection<AnyType>{
AnyType get(int idx);
AnyType set(int idx, AnyType newVal);
void add(int idx, AnyType x);
void remove(int idx);
ListIterator<AnyType> listIterator(int pos);
}
//ArrayList类的实现
public class MyArrayList<AnyType> implements Iterable<AnyType> {
private static final int DEFAULT_CAPACITY = 10;
private int theSize;
private AnyType[] theItems;
public MyArrayList(){
clear();
}
public void clear(){
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
}
public int size(){
return theSize;
}
public boolean isEmpty(){
return size()==0;
}
public void trimToSize(){
ensureCapacity(size());
}
public AnyType get(int idx){
if(idx < 0 || idx >= size()){
throw new ArrayIndexOutOfBoundsException();
}
return theItems[idx];
}
public AnyType set(int idx, AnyType newVal){
if(idx < 0 || idx >=size()){
throw new ArrayIndexOutOfBoundsException();
}
AnyType old = theItems[idx];
theItems[idx] = newVal;
return old;
}
public void ensureCapacity(int newCapacity){
if(newCapacity < theSize){
return;
}
AnyType[] old = theItems;
//创建一个泛型类型限界的数组,然后使用一个数组进行类型转换
//这将产生一个编译器警告,但在泛型集合的实现中是不可避免的
theItems = (AnyType[])new Object[newCapacity];
for(int i = 0; i < size(); i++){
theItems[i] = old[i];
}
}
public boolean add(AnyType x){
add(size(), x);
return true;
}
public void add(int idx, AnyType x){
if(theItems.length == size()){
ensureCapacity(size()*2 + 1);
}
for(int i = theSize; i > idx; i--){
theItems[i] = theItems[i-1];
}
theItems[idx] = x;
theSize++;
}
public AnyType remove(int idx){
AnyType removedItem = theItems[idx];
for(int i = idx; i<size()-1; i++){
theItems[i] = theItems[i+1];
}
theSize--;
return removedItem;
}
public java.util.Iterator<AnyType> iterator(){
return new ArrayListIterator();
}
//
private class ArrayListIterator implements java.util.Iterator<AnyType>{
private int current = 0;
public boolean hasNext(){
return current < size();
}
public AnyType next(){
if(!hasNext()){
throw new java.util.NoSuchElementException();
}
return theItems[current++];
}
public void remove(){
MyArrayList.this.remove(--current);
}
}
}
//LinkedList类的实现
public class MyLinkedList<AnyType> implements Iterable<AnyType>{
//节点内部类
private static class Node<AnyType>{
public Node(AnyType d, Node<AnyType> p, Node<AnyType> n){
data = d; prev = p; next = n;
}
public AnyType data;
public Node<AnyType> prev;
public Node<AnyType> next;
}
//构造器
public MyLinkedList(){
clear();
}
public void clear(){
beginMarker = new Node<AnyType>(null, null, null);
endMarker = new Node<AnyType>(null, beginMarker, null);
beginMarker.next = endMarker;
theSize = 0;
modCount++;
}
public int size(){
return theSize;
}
public boolean isEmpty(){
return size() == 0;
}
public boolean add(AnyType x){
add(size(), x);
return true;
}
public void add(int idx, AnyType x){
addBefore(getNode(idx), x);
}
public AnyType get(int idx){
return getNode(idx).data;
}
public AnyType set(int idx, AnyType newVal){
Node<AnyType> p = getNode(idx);
AnyType oldVal = p.data;
p.data = newVal;
return oldVal;
}
public AnyType remove(int idx){
return remove(getNode(idx));
}
private void addBefore(Node<AnyType> p, AnyType x){
Node<AnyType> newNode = new Node<AnyType>(x, p.prev, p);
newNode.prev.next = newNode;
p.prev = newNode;
theSize++;
modCount++
}
private AnyType remove(Node<AnyType> p){
p.next.prev = p.prev;
p.prev.next = p.next;
theSize--;
modCount++;
return p.data;
}
//私有方法,如果索引表示该表前半部分的一个节点,那么以向后的方向遍历该链表,否则,从终端开始向回遍历
private Node<AnyType> getNode(int idx){
Node<AnyType> p;
if(idx < 0 || idx > size()){
throw new IndexOutOfBoundsException();
}
if(idx < size()/2){
p = beginMarker.next;
for(int i = 0; i < idx; i++){
p = p.next;
}
}else{
p = endMarker;
for(int i = size(); i > idx; i--){
p = p.prev;
}
}
return p;
}
public java.util.Iterator<AnyType> iterator(){
return new LinkedListIterator();
}
private class LinkedListIterator implements java.util.Iterator<AnyType>{
private Node<AnyType> current = beginMarker.next;
private int expectedModCount = modCount;
private boolean okToRemove = false;
public boolean hasNext(){
return current != endMarker;
}
public AnyType next(){
if(modCount != expectedModCount){
throw new java.util.ConcurrentModificationException();
}
if(!hasNext()){
throw new java.util.NoSuchElementException();
}
AnyType nextItem = current.data;
current = current.next;
okToRemove = true;
return nextItem;
}
public void remove(){
if(modCount != expectedModCount){
throw new java.util.ConcurrentModificationException();
}
if(!okToRemove){
throw new IllegalStateException();
}
MyLinkedList.this.remove(current.prev);
okToRemove = false;
expectedModCount++;
}
}
private int theSize;
private int modCount = 0;
private Node<AnyType> beginMarker;
private Node<AnyType> endMarker;
}