0
点赞
收藏
分享

微信扫一扫

最简单的Java ArrayList类的实现

最简单的Java ArrayList类的实现

1、SimpleArrayList 细节概述

1、保持基础数组,数组容量,以及当前项数

2、通过扩容机制实现可变数组

3、提供集合常用的方法,size(),isEmpty(),add(),get();

4、实现Iterator类,实现SimpleArrayList 的遍历

2、源码

package org.folio.rest.base;

import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * @ProjectName: com.ketu.ilasfolio
 * @Package: org.folio.rest.base
 * @ClassName: SimpleArrayList
 * @Author: zhaokq
 * @Description:
 * @Date: 2022/1/21 11:37
 */
public class SimpleArrayList<T> implements Iterable<T> {

	//默认数组长度
    private static final int DEFAULT_CAPACITY = 10;  
	//集合大小
    private int size;
    //默认数组
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
	// 集合的数组容器
    private Object[] items;

    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

    public SimpleArrayList(int size) {
        this.size = size;
        this.items = new Object[size];
    }

    public SimpleArrayList() {
        this.items = new Object[DEFAULT_CAPACITY];
    }
    
    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void set(int i, T t) {
        items[i] = t;
    }

    public T get(int i) {
        return (T) items[i];
    }

    public boolean add(T t) {
        ensureCapacityInternal(size + 1);
        add(size(), t);
        return true;
    }

    public void add(int i, T t) {
        if (items.length == size()) {
            //扩容
            ensureCapacityInternal(size + 1);
        }
        items[i] = t;
        size++;
    }

    private void ensureCapacityInternal(int minCapacity) {
        if (items == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        ensureExplicitCapacity(minCapacity);
    }

    private void ensureExplicitCapacity(int minCapacity) {
        if (minCapacity - items.length > 0)
            grow(minCapacity);
    }

//    private void grow(int minCapacity) {
//        int oldCapacity = items.length;
//        int newCapacity = oldCapacity + (oldCapacity >> 1);
//        if (newCapacity - minCapacity < 0)
//            newCapacity = minCapacity;
//        if (newCapacity - MAX_ARRAY_SIZE > 0)
//            newCapacity = hugeCapacity(minCapacity);
//        items = Arrays.copyOf(items, newCapacity);
//    }

    private void grow(int minCapacity) {
        int oldCapacity = items.length;
        items = Arrays.copyOf(items, oldCapacity * 2);
    }

    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0)
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;
    }

    public T remove(int i) {
        T t = (T) items[i];
        for (int j = i; j < size() - 1; j++) {
            items[j] = items[j + 1];
        }
        size--;
        return t;
    }

    @Override
    public Iterator iterator() {
        return new SimpleArrayListIterator();
    }

    private class SimpleArrayListIterator implements Iterator<T> {

        private int current = 0;

        @Override
        public boolean hasNext() {
            return current < size();
        }

        @Override
        public T next() {
            if (hasNext()) {
                return (T) items[current++];
            } else {
                throw new NoSuchElementException();
            }
        }
    }



}

3、测试

    public static void main(String[] args) {
        SimpleArrayList list = new SimpleArrayList();
        long b1 = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            list.add("i = " + i);
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        long e1 = System.currentTimeMillis();
        System.out.println(e1 - b1);
//        ArrayList list = new ArrayList();
//        long b1 = System.currentTimeMillis();
//        for (int i = 0; i < 1000; i++) {
//            list.add("i = " + i);
//        }
//        for (int i = 0; i < list.size(); i++) {
//            System.out.println(list.get(i));
//        }
//        long e1 = System.currentTimeMillis();
//        System.out.println(e1-b1);
    }

4、总结

简单实现类可变数组,以及一些常用方法,只做学习ArrayList参考;
举报

相关推荐

0 条评论