59_数组_模拟ArrayList容器的底层实现_JDK源码分析ArrayList

本文介绍了一个简单的自定义ArrayList实现,包括基本操作如add、get、set等,并演示了如何使用该自定义ArrayList来存储不同类型的对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.ArrayList;

public class MyArrayList {

    private Object value[];
    private int size;

    public MyArrayList() {
        // value = new Object[16];
        this(2);
    }

    public MyArrayList(int size) {
        value = new Object[size];
    }

    public void add(Object obj) {
        value[size] = obj;
        size++;
        if (size >= value.length) {
            // 装不下了,将要扩展容量了
            int newCapacity = value.length * 2;
            Object[] newList = new Object[newCapacity];
            // System.arraycopy(src, srcPos, dest, destPos, length);
            for (int i = 0; i < value.length; i++) {
                newList[i] = value[i];
            }
            value = newList;
        }
    }

    public int size() {
        return size;
    }

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

    public int indexOf(Object obj) {
        if (obj == null) {
            return -1;
        } else {
            for (int i = 0; i < value.length; i++) {
                if (obj == value[i]) {
                    return i;
                }
            }
            return -1;
        }

    }

    public int lastIndexOf(Object obj) {
        if (obj == null) {
            return -1;
        } else {
            for (int i = value.length - 1; i >= 0; i--) {
                if (obj == value[i]) {
                    return i;
                }
            }
            return -1;// return 作用:1,返回出去值,2,结束语句。
        }

    }

    public void rangleCheck(int index) {
        if (index < 0 || index > size - 1) {// [0,size-1]
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public Object set(int index, Object object) {
        rangleCheck(index);
        Object old = value[index];
        value[index] = object;
        return old;
    }

    public Object get(int index) {
        rangleCheck(index);
        return value[index];
    }

    public static void main(String[] args) {
        MyArrayList list = new MyArrayList();
        list.add("aaa");
        list.add(new Human("高淇"));
        list.add("bbb");
        list.add("bbb");
        list.add("bbb");
        list.add("bbb");
        Human h = (Human) list.get(1);
        System.out.println(h.getName());
        System.out.println("*********************");
        System.out.println(list.get(0));
        System.out.println(list.get(1));
        System.out.println(list.get(2));
        System.out.println("*******************");
        System.out.println(list.size);

    }

}

class Human {
    private String name;

    public Human(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值