【数据结构与算法 | 基础篇】模拟ArrayList实现的“动态数组“

简介: 【数据结构与算法 | 基础篇】模拟ArrayList实现的“动态数组“

1.前言

我们将ArrayList视作动态数组,但其底层依然通过Object[]数组实现,我这里没有用到泛型,当做存储整形的动态数组.

我实现了增删查改操作,迭代器,及计算数组的长度等函数.

2.动态数组的代码实现

public class SimulateArrayTest implements Iterable<Integer>{
    //数组
    private static int[] elementDate;
    //数组实际存储数据的个数.
    private static int size = 0;
    //数组的容量
    private static int capacity = 10;
 
    public SimulateArrayTest() {
        elementDate = null;
    }
    public void add(int value) {
        if (size == 0) {
            elementDate = new int[capacity];
        }
        //存储数组的个数大于等于了数组的容量
        //则需要进行扩容操作
        if (size > capacity) {
            Dilatation();
        }
        elementDate[size++] = value;
    }
    //数组扩容操作
    public static void Dilatation() {
        int newCapacity = capacity + capacity >> 1;
        int[] newElementData = new int[newCapacity];
        System.arraycopy(elementDate, 0, newElementData, 0, size);
        capacity = newCapacity;
    }
    //查取操作
    public Integer get(int index) {
        if (index <= size - 1){
            return elementDate[index];
        }
        throw new RuntimeException();
    }
    //改数据的操作
    public void get(int index, int value) {
        if (index >= 0 && index <= size - 1) {
            elementDate[index] = value;
        }
        throw new RuntimeException();
    }
    //删除操作, 两个重载的remove方法
    public int remove(int index) {
        if (size > 0 && index >= 0 && index <= size - 1) {
            //删除的元素不是数组的末尾的元素, 我们就要把后面的数组赋值贴过来
            if (index < size - 1){
                //System.arraycopy(Object src,  int  srcPos,Object dest, int destPos,int length);
                //可以用于复制数组, 从src的srcPos位置开始复制到dest的destPos开始的位置上, 复制元素的长度为length;
                System.arraycopy(elementDate, index + 1 , elementDate, index, size - index - 1);
            }
            int temp =  elementDate[index];
            size--;
            return temp;
        }
        throw new RuntimeException();
    }
    public void remove(Integer value) {
        for (int i = 0; i < size; i++) {
            if (elementDate[i] == value) {
                if (i < size - 1) {
                    System.arraycopy(elementDate, i + 1 , elementDate, i, size - i - 1);
                }
                size--;
            }
        }
    }
    //获取数组的长度
    public int length() {
        return size;
    }
    public static int count = 0;
 
    //遍历, 迭代器
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            @Override
            public boolean hasNext() {
                if (count < size) {
                    return true;
                }
                count = 0;
                return false;
            }
 
            @Override
            public Integer next() {
                return elementDate[count++];
            }
        };
    }
    public static void Traverse(Consumer<Integer> consumer) {
        for (int i = 0; i < size; i++) {
            consumer.accept(elementDate[i]);
        }
    }
}

我们可以向动态数组中添加元素,并遍历.

@Test
    public void test() {
        SimulateArrayTest s = new SimulateArrayTest();
        s.add(12);
        s.add(23);
        s.add(34);
        s.add(45);
        s.add(56);
        s.add(67);
        s.Traverse(value -> {
            System.out.println(value);
        });
    }
 
控制台 : 
12
23
34
45
56
67
相关文章
|
2天前
|
存储 Java 索引
实现一个类似ArrayList的数据结构
实现一个类似ArrayList的数据结构
9 4
|
4天前
|
存储 Java 索引
【Java】LinkedList vs. ArrayList:Java中的数据结构选择
【Java】LinkedList vs. ArrayList:Java中的数据结构选择
12 3
|
1月前
|
存储 算法
【数据结构与算法】3、虚拟头节点、动态数组的缩容、动态数组和单链表的复杂度、数组的随机访问
【数据结构与算法】3、虚拟头节点、动态数组的缩容、动态数组和单链表的复杂度、数组的随机访问
27 0
|
1月前
|
存储 算法 Java
【数据结构与算法】1、学习动态数组数据结构(基本模拟实现 Java 的 ArrayList 实现增删改查)
【数据结构与算法】1、学习动态数组数据结构(基本模拟实现 Java 的 ArrayList 实现增删改查)
52 0
|
1月前
|
存储 算法 Java
ArrayList vs. LinkedList:数据结构之争
ArrayList vs. LinkedList:数据结构之争
27 0
|
1月前
数据结构 模拟实现ArrayList顺序表
数据结构 模拟实现ArrayList顺序表
36 0
|
1月前
|
Java
java数据结构,如何使用ArrayList和LinkedList?
java数据结构,如何使用ArrayList和LinkedList?
27 1
|
1月前
|
Java 索引
数据结构之ArrayList与顺序表(有源码剖析)(二)
数据结构之ArrayList与顺序表(有源码剖析)
48 0
|
1月前
|
存储 容器
数据结构之ArrayList与顺序表(有源码剖析)(一)
数据结构之ArrayList与顺序表(有源码剖析)(一)
52 0
|
1天前
|
算法
数据结构与算法===分治算法
数据结构与算法===分治算法