自定义 ArrayList

来源:
三产
最后修订:
2017年03月24日 14:51:56
 364
自定义 ArrayList

本文转载自:自定义 ArrayList

英文原文

Array 简介

ArrayList 类似于 Array 对象,但是当列表中的对象数量增加时,它提供了动态空间分配的功能。在 Array 对象中,我们需要在初始化时提供数组的大小,但这并不是 ArrayList 所必需的。实际上,当您初始化ArrayList时,它将自动将其容量分配为10。

这里我用 Array 对象实现ArrayList,并提供 get(index),add(object) 和 remove(index) 等基本函数。

import java.util.ArrayList;

/**
 * <p></p>
 *
 * @author 三产
 * @version 1.0
 * @date 2017-03-24
 * @QQGroup 213732117
 * @website http://www.coderknock.com
 * @copyright Copyright 2017 拿客 coderknock.com  All rights reserved.
 * @since JDK 1.8
 */
public class MyArrayList {

    private static final int SIZE_FACTOR=5;

    private Object data[];

    private int index;

    private int size;

    public MyArrayList(){
        this.data=new Object[SIZE_FACTOR];
        this.size=SIZE_FACTOR;
    }

    public void add(Object obj){
        System.out.println("index:"+this.index+"size:"+this.size+"data size:"+this.data.length);
        if(this.index==this.size-1){
            //我们需要加大data[]的大小
            increaseSizeAndReallocate();
        }
        data[this.index]=obj;
        this.index++;

    }

    private void increaseSizeAndReallocate() {
        this.size=this.size+SIZE_FACTOR;
        Object newData[]=new Object[this.size];
        for(int i=0; i<data.length;i++){
            newData[i]=data[i];
        }
        this.data=newData;
        System.out.println("***index:"+this.index+"size:"+this.size+"data size:"+this.data.length);
    }

    public Object get(int i) throws Exception{
        if(i>this.index-1){
            throw new Exception("ArrayIndexOutOfBound");
        }
        if(i<0){
            throw new Exception("Negative Value");
        }
        return this.data[i];

    }

    public void remove(int i) throws Exception{
        if(i>this.index-1){
            throw new Exception("ArrayIndexOutOfBound");
        }
        if(i<0){
            throw new Exception("Negative Value");
        }
        System.out.println("Object getting removed:"+this.data[i]);
        for(int x=i; x<this.data.length-1;x++){
            data[x]=data[x+1];
        }
        this.index--;
    }

    public static void main(String[] args) throws Exception {
        MyArrayList mal = new MyArrayList();
        mal.add("0");
        mal.add("1");
        mal.add("2");
        mal.add("3");
        mal.add("4");
        mal.add("5");
        mal.add("6");
        mal.add("7");
        mal.add("8");
        mal.add("9");

        mal.remove(5);
        System.out.println(mal.get(7));
    }

}

输出如下:

index:0size:5data size:5
index:1size:5data size:5
index:2size:5data size:5
index:3size:5data size:5
index:4size:5data size:5
***index:4size:10data size:10
index:5size:10data size:10
index:6size:10data size:10
index:7size:10data size:10
index:8size:10data size:10
index:9size:10data size:10
***index:9size:15data size:15
Object getting removed:5
8

添加泛型

这只是使用 Array 基本实现 ArrayList 的功能,只为了解其原理。还请使用 JDK 中的 ArrayList。

原文中并为实现泛型,我们就来补充下:

import java.util.ArrayList;

/**
 * <p></p>
 *
 * @author 三产
 * @version 1.0
 * @date 2017-03-24
 * @QQGroup 213732117
 * @website http://www.coderknock.com
 * @copyright Copyright 2017 拿客 coderknock.com  All rights reserved.
 * @since JDK 1.8
 */
public class MyArrayList<T> {

    private static final int SIZE_FACTOR=5;

    private Object data[];

    private int index;

    private int size;

    public MyArrayList(){
        this.data=new Object[SIZE_FACTOR];
        this.size=SIZE_FACTOR;
    }

    public void add(T obj){
        System.out.println("index:"+this.index+"size:"+this.size+"data size:"+this.data.length);
        if(this.index==this.size-1){
            //我们需要加大data[]的大小
            increaseSizeAndReallocate();
        }
        data[this.index]=obj;
        this.index++;

    }

    private void increaseSizeAndReallocate() {
        this.size=this.size+SIZE_FACTOR;
        Object newData[]=new Object[this.size];
        for(int i=0; i<data.length;i++){
        newData[i]=data[i];
    }
        this.data=newData;
        System.out.println("***index:"+this.index+"size:"+this.size+"data size:"+this.data.length);
    }

    public T get(int i) throws Exception{
        if(i>this.index-1){
        throw new Exception("ArrayIndexOutOfBound");
    }
        if(i<0){
        throw new Exception("Negative Value");
    }
        return (T) this.data[i];

    }

    public void remove(int i) throws Exception{
        if(i>this.index-1){
        throw new Exception("ArrayIndexOutOfBound");
    }
        if(i<0){
        throw new Exception("Negative Value");
    }
        System.out.println("Object getting removed:"+this.data[i]);
        for(int x=i; x<this.data.length-1;x++){
        data[x]=data[x+1];
    }
        this.index--;
    }

    public static void main(String[] args) throws Exception {
        MyArrayList<Integer> mal = new MyArrayList<Integer>();
        mal.add(0);
        mal.add(1);
        mal.add(2);
        mal.add(3);
        mal.add(4);
        mal.add(5);
        mal.add(6);
        mal.add(7);
        mal.add(8);
        mal.add(9);

        mal.remove(5);
        System.out.println(mal.get(7));
    }

}

这里其实只是修改了 add(T)、get(index) 方法。