前言
??????下面给出 ArrayList 和 LinkedList 的自定义实现,仅作为练习。
一、ArrayList
??????自己仿照着写一个 SimpleArrayList 实现类,代码也比较简单,故不做说明了,其中 SimpleItr 内部类实现了 Iterator 接口。如下:
public class SimpleArrayList<T> implements Iterable<T> {
private static final int DEFAULT_CAPACITY=10;
private int capacity=DEFAULT_CAPACITY;
private int Size;
private T[] Items;
public SimpleArrayList(){
doClear();
}
public void clear(){
doClear();
}
private void doClear(){
Size=0;
ensureCapacity(DEFAULT_CAPACITY);
}
public int size(){
return Size;
}
public boolean isEmpty(){
return size()==0;
}
public void trimToSize(){
ensureCapacity(size());
}
public T get(int idx){
if(idx<0||idx>=size())
throw new ArrayIndexOutOfBoundsException();
return Items[idx];
}
public void set(int idx,T newValue){
if(idx<0||idx>=size())
throw new ArrayIndexOutOfBoundsException();
Items[idx]=newValue;
}
public void ensureCapacity(int newCapacity){
if(newCapacity<Size)
return;
T[] old=Items;
Items=(T[])new Object[newCapacity];
for(int i=0;i<size();i++)
Items[i]=old[i];
}
public void add(T x){
add(size(),x);
}
public void add(int idx,T x){
if(Items.length==size())
ensureCapacity(size()*2+1);
for(int i=size()-1;i>idx;i--)
Items[i]=Items[i-1];
Items[idx]=x;
Size+=1;
}
public void remove(int idx){
if(idx<0||idx>=size())
throw new ArrayIndexOutOfBoundsException();
for(int i=idx;i<size()-1;i++)
Items[i]=Items[i+1];
Size-=1;
}
public Iterator<T> iterator(){
return new SimpleItr();
}
private class SimpleItr implements Iterator<T>{
private int current=0;
public boolean hasNext(){
return current<size();
}
public T next(){
if(!hasNext())
throw new NoSuchElementException();
return Items[current++];
}
public void remove(){
SimpleArrayList.this.remove(--current);
}
}
}
二、LinkedList
??????LinkedList 内部采用双链表来实现,如果操作发生在已知的情况下(端点或由迭代器指定的位置上),从而保证每个操作花费常数时间。
??????1,SimpleLinkedList 类,包含双链表和一些方法。 ??????2,Node 静态内部类。实现链表中的节点。 ??????3,SimpleItr 内部类。实现了 Iterator 接口。
public class SimpleLinkedList<T> implements Iterable<T> {
private static class Node<T>{
public T value;
public Node<T> pre;
public Node<T> next;
public Node(T value,Node<T> pre,Node<T> next){
this.value=value;this.pre=pre;this.next=next;
}
}
private int size;
private Node<T> header,tail;
public SimpleLinkedList(){
doClear();
}
public void clear(){
doClear();
}
private void doClear(){
header=new Node<T>(null,null,null);
tail=new Node<T>(null,header,null);
header.next=tail;
size=0;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size()==0;
}
private Node<T> getNode(int idx){
Node<T> tmp;
if(idx<0||idx>=size()){
throw new IndexOutOfBoundsException();
}
if(idx<size()/2){
tmp=header.next;
for(int i=0;i<idx;i++)
tmp=tmp.next;
}
else{
tmp=tail.pre;
for(int i=size()-1;i>idx;i--)
tmp=tmp.pre;
}
return tmp;
}
public T get(int idx){
return getNode(idx).value;
}
public void set(int idx,T ele){
Node<T> tmp=getNode(idx);
tmp.value=ele;
}
public void add(T x){
Node<T> newPre=tail.pre;
Node<T> tmp=new Node<>(x,newPre,tail);
tail.pre=tmp;
newPre.next=tmp;
size++;
}
public void add(int idx,T x){
Node<T> oldNode=getNode(idx);
Node<T> newPre=oldNode.pre;
Node<T> tmp=new Node<>(x,newPre,oldNode);
newPre.next=tmp;
oldNode.pre=tmp;
size++;
}
public void remove(int idx){
Node<T> node=getNode(idx);
Node<T> pre=node.pre;
Node<T> next=node.next;
pre.next=next;
next.pre=pre;
size--;
}
public Iterator<T> iterator(){
return new SimpleItr();
}
private class SimpleItr implements Iterator<T>{
private Node<T> current=header.next;
private boolean okToRemove=false;
public boolean hasNext(){
return current!=tail;
}
public T next(){
if(!hasNext())
throw new NoSuchElementException();
T nextItem=current.value;
current=current.next;
okToRemove=true;
return nextItem;
}
public void remove(){
if(!okToRemove)
throw new IllegalStateException();
Node<T> currentTmp=current.pre;
currentTmp.pre.next=current;
current.pre=currentTmp.pre;
okToRemove=false;
}
}
}
总结
??????完。
|