欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ArrayList,LinkedList,HashMap简单实现

程序员文章站 2022-03-04 14:30:03
...

ArrayList:

/**
 * 自定义ArrayList
 * @author pan
 */
public class CustomArrayList<E> {

    private Object[] elementData;
    private int size;

    private static final int DEFALT_CAPACITY = 10;

    public CustomArrayList(){
        elementData = new Object[DEFALT_CAPACITY];
    }

    public CustomArrayList(int capacity){
        if(capacity<0){
            throw new RuntimeException("初始容量不能为负数:"+capacity);
        }else if(capacity==0){
            elementData = new Object[DEFALT_CAPACITY];
        }else {
            elementData = new Object[capacity];
        }
    }

    public int size(){
        return size;
    }

    public boolean isEmpty(){
        return size==0?true:false;
    }

    public void add(E e){
        if(size == elementData.length){
            Object[] newArray = new Object[elementData.length + (elementData.length >> 1)];
            System.arraycopy(elementData,0,newArray,0,elementData.length);
            elementData = newArray;
        }
        elementData[size++] = e;
    }

    public E get(int index){
        checkElementIndex(index);
        return (E)elementData[index];
    }

    public void set(int index,E e){
        checkElementIndex(index);
        elementData[index] = e;
    }

    public void remove(int index){
        checkElementIndex(index);
        int numMoved = elementData.length-index-1;
        if(numMoved>0){
            System.arraycopy(elementData,index+1,elementData,index,numMoved);
        }
        elementData[--size] = null;
    }

    public void remove(E e){
        for(int i=0;i<size;i++){
            if (e.equals(get(i))){
                remove(i);
            }
        }
    }

    private void checkElementIndex(int index){
        if(index<0||index>size-1){
            throw new RuntimeException("索引数字不合法;"+index);
        }
    }


    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for(int i=0;i<size;i++){
            sb.append(elementData[i]+",");
        }
        sb.setCharAt(sb.length()-1,']');
        return sb.toString();
    }

}

 LinkedList:

/**
 * 自定义链表
 * @author pan
 */
public class CustomLinkedList<E> {

    private Node<E> first;
    private Node<E> last;

    private int size;

    public void add(E e){
        Node<E> node = new Node<>(e);
        if(first==null){
            node.prev = null;
            node.next = null;
            first = node;
            last = node;
        }else {
            node.prev = last;
            node.next = null;
            last.next = node;
            last = node;
        }
        size++;
    }

    public E get(int index){
        checkElementIndex(index);
        Node<E> temp = this.getNode(index);
        return temp!=null?(E)temp.item:null;
    }

    public void remove(int index){
        checkElementIndex(index);
        Node<E> temp = this.getNode(index);
        if(temp!=null){
            Node<E> up = temp.prev;
            Node<E> down = temp.next;
            if(index == 0){
                first = down;
                down.prev = null;
            }else if(index == size-1){
                last = up;
                up.next = null;
            }else {
                up.next = down;
                down.prev = up;
            }
            size--;
        }
    }

    public void insert(int index,E e){
        if(index<0||index>size){
            throw new RuntimeException("索引数字不合法;"+index);
        }
        Node<E> newNode = new Node<>(e);
        Node<E> temp = getNode(index);
        if(temp!=null){
            if(index == 0){
                first = newNode;
                newNode.next = temp;
            }else if(index == size){
                last.next = newNode;
                last = newNode;
                newNode.prev = temp;
            }else {
                Node<E> up = temp.prev;
                up.next = newNode;
                newNode.prev = up;
                newNode.next = temp;
                temp.prev = temp;
            }
            size++;
        }
    }

    private void checkElementIndex(int index){
        if(index<0||index>size-1){
            throw new RuntimeException("索引数字不合法;"+index);
        }
    }

    private Node<E> getNode(int index){
        Node<E> temp = null;
        if(index<=(size>>1)){
            temp = first;
            for (int i=0;i<index;i++){
                temp = temp.next;
            }
        }else {
            temp = last;
            for(int i=size-1;i>index;i--){
                temp = temp.prev;
            }
        }
        return temp;
    }

    @Override
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        Node<E> temp = first;
        while(temp!=null){
            sb.append(temp.item+",");
            temp = temp.next;
        }
        sb.setCharAt(sb.length()-1,']');
        return sb.toString();
    }

    private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(E e){
            this.item = e;
        }

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }
}

HashMap:

/**
 * 自定义HashMap
 * @author pan
 */
public class CustomHashMap<K,V> {

    private Node<K,V>[] table;
    private int size;

    public CustomHashMap(){
        table = new Node[16];
    }

    public int myHash(int v,int length){
        //位运算
        //System.out.println("myHash : "+ (v&(length-1)));
        //取模运算
        //System.out.println("myHash : "+ (v%length));
        return v&(length-1);
    }

    public void put(K key,V value){
        int hash = myHash(key.hashCode(), table.length);
        Node<K, V> newNode = new Node<>(hash,key,value,null);
        Node<K, V> temp = table[hash];
        Node<K, V> iterLast = null;
        boolean keyRepeat = false;
        if(temp==null){
            table[hash] = newNode;
            size++;
        }else {
            while(temp!=null){
                if(temp.key.equals(key)){
                    keyRepeat = true;
                    temp.value = value;
                    break;
                }else {
                    iterLast = temp;
                    temp = temp.next;
                }
            }
            if(!keyRepeat){
                iterLast.next = newNode;
                size++;
            }
        }
    }

    public V get(K key){
        int hash = myHash(key.hashCode(), table.length);
        V value = null;
        if (table[hash]!=null){
            Node<K, V> temp = table[hash];
            while (temp!=null){
                if (temp.key.equals(key)){
                    value = temp.value;
                    break;
                }else {
                    temp = temp.next;
                }
            }
        }
        return value;
    }

    @Override
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for(int i= 0;i<table.length;i++){
            Node<K, V> temp = table[i];
            while (temp!=null){
                sb.append(temp.key+":"+temp.value+",");
                temp = temp.next;
            }
        }
        sb.setCharAt(sb.length()-1,']');
        return sb.toString();
    }

    static class Node<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K, V> next;

        Node(int hash, K key, V value, Node<K, V> next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }
}

 

相关标签: 容器