1.迭代器接口实现

package com.zhaochao;

public interface Iterator<E> {
	 boolean  hasNext();
	 E        next();
	 boolean  delete();
	 boolean  modify(E e);
	 int      index();
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.



2.List接口实现

package com.zhaochao;





public interface List<E> {
	
    //链表大小
	int size();
	
	//链表是否为空	
	boolean isEmpty();
	
	boolean contains(Object o);

    Iterator<E> iterator();

    Object[] toArray();

    <T> T[] toArray(T[] a);

    boolean add(E e);

    boolean remove(Object o);

    boolean containsAll(List<?> c);
 
    boolean addAll(List<? extends E> c);

    boolean addAll(int index, List<? extends E> c);

    boolean removeAll(List<?> c);

    boolean retainAll(List<?> c);

    void clear();

    boolean equals(Object o);

    int hashCode();

    E get(int index);

    E set(int index, E element);

    void add(int index, E element) ;

    E remove(int index);

    int indexOf(E o);

    int lastIndexOf(E o);

    List<E> subList(int fromIndex, int toIndex);

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.

3.异常类实现

package com.zhaochao;

public class IndexOutOfBoundsException extends RuntimeException {
	 private static final long serialVersionUID = 234122996006267687L;

	    /**
	     * Constructs an <code>IndexOutOfBoundsException</code> with no
	     * detail message.
	     */
	    public IndexOutOfBoundsException() {
	        super();
	    }

	    /**
	     * Constructs an <code>IndexOutOfBoundsException</code> with the
	     * specified detail message.
	     *
	     * @param   s   the detail message.
	     */
	    public IndexOutOfBoundsException(String s) {
	        super(s);
	    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

4.链式结构单链表实现

package com.zhaochao;

import java.util.Arrays;

public class LinkList<E> implements List<E> {

	transient int length=0;
	transient Node<E> head;
	transient Node<E> last;
	
	public LinkList(){
		
	}
	public LinkList(List<E> list){
		Iterator<E> it=list.iterator();
		while(it.hasNext()){
			add(it.next());
		}
	}
	
	private static class Node<E>{
		E data;
		Node<E> next;
		Node(E e){
			this.data=e;
			this.next=null;
		}
		Node(Node<E> pre,E e){
			this.data=e;
			pre.next=next;
		}
	}
	
	
	@Override
	public int size() {
		// TODO Auto-generated method stub
		return length;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return length==0;
	}

	@Override
	public boolean contains(Object o) {
		// TODO Auto-generated method stub
		E e=(E)o;
		return indexOf(e)!=-1;
	}

	@Override
	public Iterator<E> iterator() {
		// TODO Auto-generated method stub
		return new LinkIterator();
	}
	private class LinkIterator implements Iterator<E>{
		private int nowIndex;
		
		public LinkIterator() {
			// TODO Auto-generated constructor stub
			this.nowIndex=0;
		}
		@Override
		public boolean hasNext() {
			// TODO Auto-generated method stub
			return this.nowIndex<length;
		}

		@Override
		public E next() {
			// TODO Auto-generated method stub
			E e=get(nowIndex);
			nowIndex++;
			return e;
		}

		@Override
		public boolean delete() {
			// TODO Auto-generated method stub
			remove(nowIndex);
			return true;
		}

		@Override
		public boolean modify(E e) {
			// TODO Auto-generated method stub
			set(nowIndex, e);
			return true;
		}
		@Override
		public int index() {
			// TODO Auto-generated method stub
			return nowIndex;
		}
	}
	
	

	@Override
	public Object[] toArray() {
		// TODO Auto-generated method stub
		Object[] obj=new Object[length-1];
		Iterator it=this.iterator();
		int i=0;
		while(it.hasNext()){
			obj[i++]=it.next();
		}
		return obj;
	}

	@Override
	public <T> T[] toArray(T[] a) {
		// TODO Auto-generated method stub
	     if (a.length < length)
	            // Make a new array of a's runtime type, but my contents:
	            return (T[]) Arrays.copyOf(a, length, a.getClass());
	        a=(T[])toArray();
	        if (a.length > length)
	            a[length] = null;
	        return a;
	}

	@Override
	public boolean add(E e) {
		// TODO Auto-generated method stub
		addLast(e);
		return false;
	}

	@Override
	public boolean remove(Object o) {
		// TODO Auto-generated method stub
		while(contains(o)){
			E e=(E)o;
			remove(indexOf(e));
		}
		
		return true;
	}

	@Override
	public boolean containsAll(List<?> c) {
		// TODO Auto-generated method stub
		boolean flag=true;
		Iterator it=c.iterator();
		while(it.hasNext()){
			flag=flag&&contains(it.next());
		}
		return flag;
	}

	@Override
	public boolean addAll(List<? extends E> c) {
		// TODO Auto-generated method stub
		Iterator it=c.iterator();
		while(it.hasNext()){
			add((E)it.next());
		}
		return true;
	}

	@Override
	public boolean addAll(int index, List<? extends E> c)  {
		// TODO Auto-generated method stub
		Iterator it=c.iterator();
		while(it.hasNext()){
			add(index++,(E)it.next());
		}
		return true;
	}

	@Override
	public boolean removeAll(List<?> c) {
		// TODO Auto-generated method stub
		Iterator it=c.iterator();
		while(it.hasNext()){
			if(contains(it.next()))
				remove(it.next());
		}
		return true;
	}

	@Override
	public boolean retainAll(List<?> c) {
		// TODO Auto-generated method stub
		Iterator it=this.iterator();
		while(it.hasNext()){
			E e=(E) it.next();
			if(!c.contains(e))
				remove(e);
		}
		return true;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		head=null;
		last=null;
		length=0;
	}

	@Override
	public E get(int index) {
		// TODO Auto-generated method stub
		checkRomoveIndex(index);
		Node<E> node=head;
		for(int i=0;i<index;i++){
			node=node.next;
		}
		return node.data;
	}

	@Override
	public E set(int index, E element) {
		// TODO Auto-generated method stub
		Node<E> node=getNode(index);
		node.data=element;
		return element;
	}

	@Override
	public void add(int index, E element)  {
		// TODO Auto-generated method stub
		checkAddIndex(index);
		if(index==0){
			Node<E> h=head;
			Node<E> node=new Node<E>(element);
			head=node;
			head.next=h;
			
		}else{
			Node<E> node=getNode(index-1);
			Node<E> newNode=new Node<E>(element);
			newNode.next=node.next;
			node.next=newNode;
		}
		length++;
	}

	@Override
	public E remove(int index) {
		// TODO Auto-generated method stub
		checkRomoveIndex(index);
		E e;
		if(index==0){
			 e=head.data;
			head=head.next;
		}else{
			Node<E> node=getNode(index-1);
			e=node.next.data;
			node.next=node.next.next;
		}
		length--;
		return e;
	}

	@Override
	public int indexOf(E o) {
		// TODO Auto-generated method stub
		
		for(int i=0;i<length;i++){
			if(get(i).equals(o))
				return i;
		}
		return -1;
	}

	@Override
	public int lastIndexOf(E o) {
		// TODO Auto-generated method stub
		List ll=new LinkList();
		for(int i=0;i<length;i++){
			if(get(i).equals(o))
				ll.add(i);
		}
		return (int) ll.get(ll.size()-1);
	}

	@Override
	public List<E> subList(int fromIndex, int toIndex) {
		// TODO Auto-generated method stub
		checkRomoveIndex(fromIndex);
		checkRomoveIndex(toIndex);
		List<E> ll=new LinkList<E>();
		for(int i=fromIndex;i<=toIndex;i++)
			ll.add(get(i));
		return ll;
	}
	
	private void addLast(E e){
		if(head==null){
			Node<E> node=new Node<E>(e);
			head=node;
			last=node;
		}else{
			Node<E> l=last;
			Node<E> node=new Node<E>(l,e);
			last=node;
			l.next=last;
		}
		length++;
	}
	private Node<E> getNode(int index){
		checkRomoveIndex(index);
		Node<E> node=head;
		for(int i=0;i<index;i++){
			node=node.next;
		}
		return node;
	}
		
	private void checkRomoveIndex(int index){
		if(index<0||index>=length){
			throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
		}
	}
	private void checkAddIndex(int index)  {
		if(index<0||index>length){
			throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
		}
	}
	private String outOfBoundsMsg(int index){
		return "index:"+index+" length:"+length;
	}
	
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
  • 270.
  • 271.
  • 272.
  • 273.
  • 274.
  • 275.
  • 276.
  • 277.
  • 278.
  • 279.
  • 280.
  • 281.
  • 282.
  • 283.
  • 284.
  • 285.
  • 286.
  • 287.
  • 288.
  • 289.
  • 290.
  • 291.
  • 292.
  • 293.
  • 294.
  • 295.
  • 296.
  • 297.
  • 298.
  • 299.
  • 300.
  • 301.
  • 302.
  • 303.
  • 304.
  • 305.
  • 306.
  • 307.
  • 308.
  • 309.
  • 310.
  • 311.
  • 312.
  • 313.
  • 314.
  • 315.
  • 316.
  • 317.
  • 318.
  • 319.
  • 320.
  • 321.
  • 322.
  • 323.
  • 324.
  • 325.
  • 326.
  • 327.
  • 328.
  • 329.
  • 330.






5.测试

package com.zhaochao;


public class main {


	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		List<Test> ls=new LinkList<Test>();
		Test t=new Test();
		for(int i=0;i<10;i++)
			ls.add(t);
		Iterator it=ls.iterator();
		while(it.hasNext()){
			System.out.println(it.next());
		}
	
	}

}


class Test{
	public static int a=0;
	public String toString(){
		return String.valueOf(a++);
	}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.






6.测试结果

0
1
2
3
4
5
6
7
8
9
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.