手写ArrayList,LinkedList

来源:互联网 发布:最好用的安卓优化软件 编辑:程序博客网 时间:2024/06/06 03:40

虽然学习Java很长时间了,但是对数据结构一直很痛恨,更何况手写Collections之类的了,这次实在是没办法,所以看看书,上网看看别人怎么写的,扒了过来了,记个笔记方便以后复习,\(^o^)/~

ArrayList是比较好写,比较容易看懂的......

package com.juan;

public class MyArrayList<AnyType>{
    
    private static final int DEFAULT_CAPACITY = 10;
    private int theSize;
    private AnyType[] theItems;
    
    /**
     * 返回ArrayList的长度
     * @return
     */
    public int size(){
        return theSize;
    }
    
    public boolean isEmpty(){
        return size() == 0;
    }
    
    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 MyArrayList(){
        clear();
    }
    
    public void clear(){
        theSize = 0;
        ensureCapacity(DEFAULT_CAPACITY);
    }
    
    public void trimToSize(){
        ensureCapacity(size());
    }
    
    public AnyType get(int idx){
        if(idx < 0 || idx >= size()){
            return null;
        }else{
            return theItems[idx];
        }
    }
    
    public AnyType set(int idx, AnyType newVal){
        if(idx < 0 || idx >= size()){
            return null;
        }else{
            AnyType old = theItems[idx];
            theItems[idx] = newVal;
            return old;
        }
        
    }
    
    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++){ //为什么不是size()
            theItems[i] = theItems[i + 1];
        }
        theSize--;
        return removedItem;
    }
    
    
    
}

-----------------------------------------------------------------------------------------------------------------------

LinkedList折磨我很久,光看代码没办法消化,我就在纸上画了画,勉强过关了

package com.juan;

public class MyLinkedList<AnyType> {
    private static class Node<AnyType>{
        public AnyType data;
        public Node<AnyType> prev;
        public Node<AnyType> next;
        public Node(AnyType d, Node<AnyType> p, Node<AnyType> n){
            data = d;
            prev = p;
            next = n;
        }        
    }
    
    private int theSize;
    private int modCount;
    private Node<AnyType> beginMarker;
    private Node<AnyType> endMarker;
    
    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;
    }
    
    public int size(){
        return theSize;
    }
    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;
    }
    
    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 Node<AnyType> getNode(int idx){
        Node<AnyType> p;
        
        if(idx < 0 || idx > size()){
            System.out.println("IndexOutOfBoundsException");
        }
        
        if(idx <= size()/2){
            System.out.println(idx);
            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 boolean find(AnyType x){
        Node<AnyType> p = beginMarker.next;
        for(int i = 0; i < size(); i++){
            if(p.data == x){
                return true;
            }
            p = p.next;
        }
        return false;        
    }
    
/**这个方法还有点问题,打印前面多了个null,昨天实在太困了,还没来得及完善,待续......**/   

public String toString(){
        String s = null;
        Node<AnyType> p = beginMarker.next;
        for(int i = 0; i < size(); i++){
            s += p.data +",";
            p = p.next;
        }
        return s;
    }
    
    
}




原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 35岁无稳定工作怎么办 机械手表走的快怎么办 机械表发条紧了怎么办 两只乌龟互相咬怎么办? 乌龟鼻子摔烂了怎么办 剃刀龟脖子肿了怎么办 遇见有戾气的人怎么办 身上的寒气太重怎么办 身体里寒气太重怎么办 做人事招不到人怎么办 苹果6cpu坏了怎么办 苹果6s升级不了怎么办 冬天打游戏手冷怎么办 漂流瓶不能用了怎么办 感冒鼻子闻不到味道怎么办 胃难受想吐头晕怎么办 心口窝堵得慌怎么办 嘴巴里苦的很怎么办 怀孕了嘴巴好苦怎么办 嘴巴没味道想吐怎么办 手机流量不够用怎么办移动 sd卡图片不显示怎么办 苹果忘了id账号怎么办 苹果id号忘记了怎么办 7icloud存储满了怎么办 苹果6icloud满了怎么办 电脑内存槽坏了怎么办 苹果7照片删不了怎么办 屋里太冷怎么办小妙招 天气太热,没空调怎么办 8岁儿童发烧39度怎么办 4岁儿童发烧39度怎么办 6岁儿童发烧39度怎么办 手机被晒得很烫怎么办 子宫肌瘤引起的贫血怎么办 月子没做好腰疼怎么办 狗狗屁股流血水怎么办 狗狗屁股在流血怎么办 劳累引起的腰疼怎么办 心口发闷堵的慌怎么办 刨腹产后肚子大怎么办