Java学习笔记

Java编程练习——ArrayList

练习java编程,自己简单实现ArraryList,代码如下:

/**
 *MyArrayList class
 * @author zc
 *
 */
public class MyArrayList<T> {

    /**
     * DEFAULT_CAPACITY 默认长度
     */
    private static final int DEFAULT_CAPACITY = 10;
    /**
     * theSize 大小/元素个数
     */
    private int theSize;
    /**
     * theItems 数组
     */
    private T[] theItems;

    /**
     * 清空 初始化
     */
    public void clear(){
        theSize = 0;
        theItems = (T[]) new Object[DEFAULT_CAPACITY];
    }

    /**
     * 构造函数
     */
    public MyArrayList(){
        clear();
    }

    /**
     * 计算大小
     * @return 大小/元素个数
     */
    public int size(){
        return theSize;
    }

    /**
     *判断是否为空
     * @return 空为真/非空为假
     */
    public boolean isEmpty(){
        return size()==0;
    }

    /**
     * 容量扩充
     * @param minCapacity 所需最小空间
     */
    public void ensureCapacity(int minCapacity){
        if(minCapacity <= theItems.length) {
            return;
        }
        T[] old = theItems;
        int newCapacity = (theItems.length * 3)/2 + 1;
        if(newCapacity < minCapacity) {
            newCapacity = minCapacity;
        }
        theItems = (T[]) new Object[newCapacity];
        for(int i = 0; i < size(); i++ )
        {
            theItems[i] = old[i];
        }
    }

    /**
     * 根据元素下标获取元素值
     * @param index 元素下标
     * @return 元素值
     */
    public T get(int index){
        if(index >= size() || index < 0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return theItems[index];
    }

    /**
     * 修改固定位置的元素值
     * @param index 下标
     * @param newVal 新值
     * @return 被修改的元素值
     */
    public T set(int index, T newVal){
        if(index >= size() || index < 0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        T old = theItems[index];
        theItems[index] = newVal;
        return old;
    }

    /**
     * 在下标处添加元素
     * @param index 下标
     * @param element 元素值
     */
    public void add(int index, T element){
        if(index >= size() || index < 0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        ensureCapacity(size()+1);
        for(int i = theSize; i > index;i--)
        {
            theItems[i] = theItems[i-1];
        }
        theItems[index] = element;
        theSize++;
    }

    /**
     * 末尾添加元素
     * @param element 元素值
     * @return 添加位置
     */
    public int add(T element){
        ensureCapacity(size()+1);
        theItems[theSize++] = element;
        return size()-1;
    }

    /**
     * 获取元素值第一次出现的位置
     * @param element 元素值
     * @return 下标
     */
    public int indexOf(T element) {
        int index = -1;
        for(int i = 0; i < size(); i++)
        {
            if(element.equals(theItems[i]))
            {
                index = i;
                break;
            }
        }
        return index;
    }

    /**
     * 获取元素最后一次出现位置
     * @param element 元素值
     * @return 下标
     */
    public int lastIndexOf(T element){
        int index = -1;
        for(int i = size()-1; i >= 0; i--)
        {
            if(element.equals(theItems[i]))
            {
                index = i;
                break;
            }
        }
        return index;
    }

    /**
     * 删除下标位置元素
     * @param index 下标值
     * @return 被删除元素值
     */
    public T remove(int index){
        if(index >= size() || index < 0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        T removeItem = theItems[index];
        for(int i = index; i < size()-1; i++)
        {
            theItems[i]=theItems[i+1];
        }
        theItems[theSize] = null;
        theSize--;
        return removeItem;
    }

    /**
     * 测试
     * @param args 固定参数
     */
    public static void main(String[] args) {
        MyArrayList<String > list = new MyArrayList<String>();
        System.out.println(list.isEmpty());
        list.add("123");
        list.add("234");
        list.add("345");
        list.add(1, "qwe");
        list.add("123");
        list.add("123");
        list.add("123");
        list.add("123");
        list.add("123");
        list.add("123");
        list.add("123");
        list.add("123");
        list.add("123");
        list.add("123");
        list.set(5, "wer");
        list.remove(0);

        System.out.println(list.isEmpty());
        System.out.println(list.indexOf("123") + "  " + list.lastIndexOf("123"));
        System.out.println(list.size());
        String tmp =list.get(list.size() - 1);
        System.out.println(tmp);
    }

}

属性

  • private T[] theItems: 实现MyArrayList的底层数组。
  • private static final int DEFAULT_CAPACITY: 数组的默认长度。
  • private int theSize: 数组内元素个数。

方法

  • size()
public int size(){
        return theSize;
    }

有专门标识元素个数的属性theSize,所以获取大小的方法实现简单,直接返回theSize的值即可。。

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

判断是否为空也依赖于theSize属性,当数组内没有元素时即为空,否则不为空。

  • ensureCapacity()
public void ensureCapacity(int minCapacity){
        if(minCapacity <= theItems.length) {
            return;
        }
        T[] old = theItems;
        int newCapacity = (theItems.length * 3)/2 + 1;
        if(newCapacity < minCapacity) {
            newCapacity = minCapacity;
        }
        theItems = (T[]) new Object[newCapacity];
        for(int i = 0; i < size(); i++ )
        {
            theItems[i] = old[i];
        }
    }

元素的添加会是数组内的元素个数增加,当超出数组的长度时,需要进行容量的扩充来确保数组不会越界。容量扩充的具体实现步骤为:判断所需的最小容量(原元素个数+要添加的元素个数)是否满足,即所需长度有没有超过数组的长度,若不超过,结束返回;将数组元素提取出来;扩容1.5倍;扩容后还不满足,则扩至所需最短长度;将数组元素放回。

  • add()

两种添加元素的方式:
*一是根据下标插入元素,即在指定下标位置添加新元素,其他元素整体后移。

public void add(int index, T element){
        if(index >= size() || index < 0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        ensureCapacity(size()+1);
        for(int i = theSize; i > index;i--)
        {
            theItems[i] = theItems[i-1];
        }
        theItems[index] = element;
        theSize++;
    }

进行元素添加操作时,要先判断数组是否越界,接着进行扩容的判断,然后将下标值后的数据依次后移,之后将元素值插入,最后进行元素个数的增加操作。
*二是直接添加元素,这就是在末尾填加新元素。

public int add(T element){
        ensureCapacity(size()+1);
        theItems[theSize++] = element;
        return size()-1;
    }

在末尾添加元素相对简单,扩容后直接赋值并把元素个数加一即可。

  • remove()
public T remove(int index){
        if(index >= size() || index < 0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        T removeItem = theItems[index];
        for(int i = index; i < size()-1; i++)
        {
            theItems[i]=theItems[i+1];
        }
        theItems[theSize] = null;
        theSize--;
        return removeItem;
    }

这个方法返回了被删除元素的值,所以需要先记录了一下被删除元素,然后将后续元素前移,之后将最后一个元素置空减少元素个,最后返回被删除的元素值。注意:每次进行涉及到数组下标的操作时,都要先进行数组是否越界的判断(下同)。

  • get()
public T get(int index){
        if(index >= size() || index < 0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return theItems[index];
    }

根据下标获取元素值,只需要直接去找数组中对应下标的元素即可。

  • set()
public T set(int index, T newVal){
        if(index >= size() || index < 0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        T old = theItems[index];
        theItems[index] = newVal;
        return old;
    }

此方法是修改对应下标位置的数组内元素值,需要返回被修改元素值,所以也是需要先记录一下对应位置原元素的值,然后赋新值,返回旧值。

  • indexOf()
public int indexOf(T element) {
        int index = -1;
        for(int i = 0; i < size(); i++)
        {
            if(element.equals(theItems[i]))
            {
                index = i;
                break;
            }
        }
        return index;
    }

获取元素第一次出现位置,需要从头遍历数组,找到所需元素后停止即可。

  • lastIndexOf()
public int lastIndexOf(T element){
        int index = -1;
        for(int i = size()-1; i >= 0; i--)
        {
            if(element.equals(theItems[i]))
            {
                index = i;
                break;
            }
        }
        return index;
    }

与indexOf()类似,从数组最后开始遍历即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值