ArrayList和LinkedList的自我实现

来源:互联网 发布:b2b平台排名数据 编辑:程序博客网 时间:2024/06/10 02:55

ArrayList的实现:

public class MyArrayList {    private Object elementData[];    private int size;    public MyArrayList(){this(10);};    public MyArrayList(int initialCapacity){        if(initialCapacity<0){            try {                throw new Exception();            } catch (Exception e) {                e.printStackTrace();            }        }        elementData = new Object[initialCapacity];    }    //实现元素的添加    public void add(Object obj){        //数据的扩容和拷贝        if(size<0||size>=elementData.length){            int newCapacity = 2*size+2;            Object[] value = new Object[newCapacity];            System.arraycopy(elementData, 0, value, 0, elementData.length);            elementData = value;        }        elementData[size] = obj;        size++;    }    public void add(int index,Object obj){        Rangecheck(index);        System.arraycopy(elementData, index, elementData, index+1, size-index);        elementData[index] = obj;    }    //根据索引取值    public Object get(int index){        Rangecheck(index);        return elementData[index];    }    //实现移除方法    public void remove(int index){        Rangecheck(index);        int numMove = size - index - 1;        if(numMove>0){            System.arraycopy(elementData, index+1, elementData, index, numMove);        }        elementData[size--] = null;    }    public void remove(Object obj){        for(int i=0;i<size;i++){            if(elementData[i].equals(obj)){                this.remove(i);            }        }    }    //实现取代方法    public void set(int index,Object obj){        Rangecheck(index);        elementData[index] = obj;    }    //检测是否越键    public void Rangecheck(int index){        if(index<0||index>=size){            try {                throw new Exception();            } catch (Exception e) {                e.printStackTrace();            }        }    }    //返回容器的大小    public int size(){        return size;    }    public static void main(String[] args) {        MyArrayList s = new MyArrayList();        s.add("aa");        s.add("bb");        s.add("cc");//      s.remove("aa");        s.set(0, "dd");        s.add(1, "ee");        System.out.println(s.get(1));    }}

LinkedList的实现:

package cn.feng.test1;/** * 测试Linkedlist * @author Administrator */public class test02 {    Node first;    Node last;    private int size;    /**链表数目方法**/    public int size(){        return size;    }    /**添加元素方法**/    public void add(Object obj){        Node n = new Node();        if(first==null){            n.setPrevious(null);            n.setObj(obj);            n.setNext(null);            first = n;            last = n;        }else{            n.setPrevious(last);            n.setObj(obj);            n.setNext(null);            last.setNext(n);            last = n;        }        size++;    }    /**替换方法**/    public void set(int index,Object obj){        Node temp = null;        if(first!=null){            temp = first;            for(int i = 0;i<index;i++){                temp = temp.next;            }        }        temp.obj = obj;    }    /**删除方法**///-->其实这个方法有问题,对于第一个对象的移除后,再去第一个对象会报错空指针    public void remove(int index){        Node temp = null;        if(first!=null){            temp = first;            for(int i = 0;i<index;i++){                temp = temp.next;            }        }        Node up = temp.previous;        Node dowm = temp.next;        up.next = dowm;        dowm.previous = up;        size--;    }    /**插入方法**/    public void add(int index,Object obj){        Node temp = null;        if(first!=null){            temp = first;            for(int i = 0;i<index;i++){                temp = temp.next;            }        }        Node newNode = new Node();        Node up = temp.previous;        up.next = newNode;        newNode.previous = up;        newNode.obj = obj;        newNode.next = temp;        temp.previous = newNode;        size++;    }    /**查找方法**/    public Object get(int index){        Node temp = null;        if(first!=null){            temp = first;            for(int i = 0;i<index;i++){                temp = temp.next;  // 先不管这个节点有什么内容,主要遍历是为了找到这个节点            }        }        return temp.obj;    }    public static void main(String[] args) {        test02 a = new test02();        a.add("aaa");        a.add("bbb");        a.add("rrr");//      a.add(1, "ccc");        a.remove(1);//      a.set(1, "ddd");        System.out.println(a.get(1));    }}/** 设置节点**/class Node{    Node previous;    Object obj;    Node next;    public Node(){      }    public Node(Node previous, Object obj, Node next) {        super();        this.previous = previous;        this.obj = obj;        this.next = next;    }    public Node getPrevious() {        return previous;    }    public void setPrevious(Node previous) {        this.previous = previous;    }    public Object getObj() {        return obj;    }    public void setObj(Object obj) {        this.obj = obj;    }    public Node getNext() {        return next;    }    public void setNext(Node next) {        this.next = next;    }}