1. 程式人生 > >Java三大集合(2)

Java三大集合(2)

本文采用陣列實現ArrayList集合,採用ArrayList實現HashMap集合,採用自定義實體類實現LinkedList集合

一.ArrayList集合

package day1122;

public class ArrayList<Element> {

    private Object[] array = new Object[10];
    private int size = 0;

    public ArrayList() {
        array = new Object[2];
    }
    public ArrayList(int num) {
        array = new
Object[num]; } public int size() { return size; } public void biggerSize() { int length = array.length; if(size>=length) { Object[] array2 = new Object[length+length/2]; System.arraycopy(array,0,array2,0,array.length); array = array2; } } public
void add(Element element) { biggerSize(); array[size] = element; size++; } public void remove(int index) { if (index>=size) { throw new ArrayIndexOutOfBoundsException(); } size--; for(int i=index;i<size;i++) { array[i] = array[i+1
]; } array[size] = null; } public Element get(int index) { if (index>=size) { throw new ArrayIndexOutOfBoundsException(); } return (Element)array[index]; } public void set(int index,Element element) { if (index>=size) { throw new ArrayIndexOutOfBoundsException(); } array[index] = element; } public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); long start = System.currentTimeMillis(); for(int i=0;i<10000000;i++) { list.add(i); } long end = System.currentTimeMillis(); System.out.println(end-start); } }

二.HashMap集合

package day1122;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;

public class HashMap<Key,Value> {

    //儲存key值,value值和hashCode值
    private ArrayList<Key> keys = new ArrayList<Key>();
    private ArrayList<Value> values = new ArrayList<Value>();
    private ArrayList<Integer> codes = new ArrayList<Integer>();

    public void push(Key key,Value value) {
        int code = key.hashCode();
        //只有hashCode一樣,並且key相同,才重設value
        for(int i=0;i<keys.size();i++) {
            if(code==codes.get(i) && key.equals(keys.get(i))) {
                values.set(i, value);
                return;
            }
        }
        //否則3個集合同時新增元素
        keys.add(key);
        values.add(value);
        codes.add(key.hashCode());
    }

    public Value get(Key key) {
        //查出所有hashCode相同的索引
        int num = codes.indexOf(key.hashCode());
        while(num>=0) {
            //逐一比對key是否相同,相同直接返回
            if(key.equals(keys.get(num))) {
                return values.get(num);
            }
            //不符合的索引暫時設為null
            codes.set(num, null);
            num = codes.indexOf(key.hashCode());
        }
        //將所有為null的索引重設回來
        num = codes.indexOf(null);
        while(num>=0) {
            codes.set(num, key.hashCode());
            num = codes.indexOf(null);
        }
        return null;
    }

    public void remove(Key key) {
        //查出所有hashCode相同的索引
        int num = codes.indexOf(key.hashCode());
        while(num>=0) {
            //逐一比對key是否相同,相同則同時刪除3個集合的元素,結束方法
            if(key.equals(keys.get(num))) {
                codes.remove(num);
                keys.remove(num);
                values.remove(num);
                break;
            }
            //不符合的索引暫時設為null
            codes.set(num, null);
            num = codes.indexOf(key.hashCode());
        }
        //將所有為null的索引重設回來
        num = codes.indexOf(null);
        while(num>=0) {
            codes.set(num, key.hashCode());
            num = codes.indexOf(null);
        }

    }
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<Integer, String>();
        Integer a = new Integer(1);
        Integer b = new Integer(1);

        map.push(a, "哈哈哈");
        map.push(b, "喲喲喲");
        System.out.println(map.get(1));
    }

}

三.LinkedList集合

package day1122;

public class LinkedList<Element> {
    //初始值
    private Entity<Element> head = new Entity<Element>();
    private int size = 0;

    public void add(Element element) {
        Entity<Element> end = head.getLast();
        Entity<Element> entity = new Entity<Element>(end,element,head);
        end.setNext(entity);
        head.setLast(entity);
        size++;
    }

    public void add(int index,Element element) {
        Entity<Element> current = getEntity(index);
        Entity<Element> last = current.getLast();
        Entity<Element> entity = new Entity<Element>(last,element,current);
        last.setNext(entity);
        current.setLast(entity);
        size++;
    }

    public Element get(int index) {
        return getEntity(index).getElement();
    }

    public void set(int index,Element element) {
        getEntity(index).setElement(element);
    }
    public void remove(int index) {
        Entity<Element> current = getEntity(index);
        Entity<Element> last = current.getLast();
        Entity<Element> next = current.getNext();
        last.setNext(next);
        next.setLast(last);
        size--;
    }
    //根據索引獲取元素物件
    public Entity<Element> getEntity(int index) {
        if (index>=size || index<0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        Entity<Element> current = head;
        //向右遍歷的次數
        int right = index+1;
        //向左遍歷的次數
        int left = size+1-right;
        //比較大小,決定遍歷的方向
        if(right<left) {
            for(int i=0;i<right;i++){
                current = current.getNext();
            }
        } else {
            for(int i=0;i<left;i++){
                current = current.getLast();
            }
        }
        return current;
    }

    public int size() {
        return size;
    }

    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<Integer>();

        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.remove(1);
        list.add(2,5);
        for(int i=0;i<list.size();i++) {
            System.out.println(list.get(i));
        }
    }
}

class Entity<Element> {

    private Entity<Element> last;
    private Element element;
    private Entity<Element> next;

    public Entity(Entity<Element> last, Element element, Entity<Element> next) {
        this.last = last;
        this.element = element;
        this.next = next;
    }
    public Entity() {
        this.last = this;
        this.element = null;
        this.next = this;
    }
    public Entity<Element> getLast() {
        return last;
    }
    public void setLast(Entity<Element> last) {
        this.last = last;
    }
    public Element getElement() {
        return element;
    }
    public void setElement(Element element) {
        this.element = element;
    }
    public Entity<Element> getNext() {
        return next;
    }
    public void setNext(Entity<Element> next) {
        this.next = next;
    }
}