Java300学习笔记(7)—— 容器 ArrayList

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012292754/article/details/86513522

1 Collection

  • java.util.Collection 接口是 Collection 层次结构中的根接口,它定义了一些最基本的访问方法;
    在这里插入图片描述

2 测试 ArrayList

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
* ArrayList : 底层用数组实现。查询快;修改,插入,删除慢
* LinkedList: 底层实现是链表;查询慢,修改,插入,删除快
*
* Vector: 底层用数组,线程安全
*
* */

public class Test01 {
    public static void main(String[] args) {
        List list = new ArrayList();

        list.add("AAA");
        list.add(1234);
        list.add(new Date());
        System.out.println(list.size()); // 3

        List list2 = new ArrayList();
        list2.add("BBB");
        list2.add("CCC");

        list.add(list2);
        System.out.println(list.size()); // 4

        String str = (String) list.get(0);
        System.out.println(str);

        list.set(1,"aaa");



    }
}

3 自己实现 ArrayList

package day04.collection;


public class MyArrayList {

    private Object[] elementData;

    private int size;

    public MyArrayList() {
        this(10);
    }

    public MyArrayList(int initialCapacity) {

        if (initialCapacity < 0) {
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        elementData = new Object[initialCapacity];
    }

    public int size() {
        return size;
    }

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

    public void add(Object obj) {

        if (size == elementData.length) {
            Object[] newArray = new Object[size * 2 + 1];
            System.arraycopy(elementData, 0, newArray, 0, elementData.length);
            elementData = newArray;
        }

        elementData[size] = obj;
        size++;
    }

    public Object get(int index) {
        if (index < 0 || index >= size) {
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return elementData[index];
    }

    public void remove(int index) {
        if (index < 0 || index >= size) {
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        int numMoved = size - index - 1;
        if (numMoved > 0) {
            System.arraycopy(elementData, index + 1, elementData, index, numMoved);
        }

        elementData[--size] = null;

    }

    public void remove(Object obj) {
        for (int i = 0; i < size; i++) {
            if (get(i).equals(obj)) {
                remove(i);
            }
        }
    }

    public Object set(int index, Object obj) {
        rangeCheck(index);
        Object oldValue = elementData[index];
        elementData[index] = obj;
        return oldValue;
    }

    public void add(int index, Object obj) {

        rangeCheck(index);

        ensureCapacity();
        System.arraycopy(elementData, index, elementData, index + 1,
                size - index);
        elementData[index] = obj;
        size++;

    }

    private void ensureCapacity(){
        if (size == elementData.length) {
            Object[] newArray = new Object[size * 2 + 1];
            System.arraycopy(elementData, 0, newArray, 0, elementData.length);
            elementData = newArray;
        }

    }

    private void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        MyArrayList list = new MyArrayList(3);
        list.add("AAA");
        list.add("BBB");
        list.add("CCC");
        list.add("DDD");

        System.out.println(list.size());

    }
}

猜你喜欢

转载自blog.csdn.net/u012292754/article/details/86513522