自定义ArrayList

ArrayList是通过数组实现的。

实现ArrayList类中的以下功能:

  1. size():列表的大小
  2. isEmpty():判断列表是否为空
  3. clear():清空整个列表
  4. get(int index):获取下标为index的值
  5. set(int index,Object value):修改下标为index的值
  6. add(Object value):向列表中(也就是在列表的最后)添加数据
  7. remove(int index):删除下标为index的值
  8. 实现ArrayList的迭代器(Iterator),实现hasnext()、next()、remove()方法。注意:这里并没有实现remove()方法必须在next()方法之后调用的逻辑
package com.test.test;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Test2 {

    public static void main(String[] args) {
        MyArrayList<String> mList = new MyArrayList<String>();
        mList.add("我");
        mList.add("是");
        mList.add("大");
        mList.add("小");
        mList.add("牛");
        mList.add("奶");

//      System.out.println(mList.isEmpty());
//      System.out.println(mList.size());

        mList.set(0, "你");
        for (int i = 0; i < mList.size(); i++) {
            String value = (java.lang.String) mList.get(i);
            System.out.println(value);
        }

//      Iterator<String> iterator = mList.iterator();
//      while(iterator.hasNext()){
//          System.out.println(iterator.next());
//      }

//      mList.clear();
//      System.out.println(mList.size());



    }

}

/**
 * 自定义的ArrayList类
 * 2015年10月19日 下午4:59:05
 * @author 张耀晖
 *
 * @param <String>
 */
class MyArrayList<String> implements Iterable<String> {

    private int theSize;
    private Object[] theItems;
    private static final int DEFAULT_LENGTH = 10;//设置默认的数组的大小为10

    // 清除数据
    public void clear() {
        theSize = 0;
        ensureCapacity(DEFAULT_LENGTH);
    }

    // list大小,返回list的大小
    public int size() {
        return theSize;
    }

    // list是否为空,返回是否为空的标志
    public boolean isEmpty() {
        return size() == 0;
    }

    //获取list中的index对应的值,返回获取到的值
    public Object get(int index) {
        if (index < 0 || index >= size()) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return theItems[index];
    }

    //设置某个位置的元素的值,返回原来位于该位置的元素
    public Object set(int index,Object value){
        if(index<0||index>=size()){
            throw new ArrayIndexOutOfBoundsException();
        }
        Object oldItem = theItems[index];
        theItems[index] = value;

        return oldItem;
    }

    //在数组的最后的位置添加元素,返回添加是否成功的标志
    public boolean add(Object value){
        add(size(), value);//这里的size()其实就是list所拥有的最后的下标+1的位置
        return true;
    }

    //往list中添加值,在index位置处添加value
    public void add(int index,Object value) {
        ensureCapacity(index);
        for(int i=index;i<size();i++){
            theItems[i+1] = theItems[i];//将添加位置以后的数据向后移动
        }
        theItems[index] = value;//设置添加位置的值
        theSize++;//列表的大小增加1
    }

    //移除index下标对应的值
    public Object remove(int index) {
        Object removeItem = theItems[index];//获取删除位置的原来的值
        for(int i=index;i<size()-1;i++){
            theItems[i] = theItems[i+1];//将删除位置以后的数据向前移动
        }
        theSize--;//列表的大小减少1
        return removeItem;
    }

    // 确定数组的大小
    public void ensureCapacity(int newCapacity) {
        if (newCapacity < theSize) {
            return;
        } else {//如果需要的容量大于原先数组的容量
            Object[] old = theItems;//保存原来的数组内容
            theItems = new Object[newCapacity+1];//新建一个扩容数组
            for (int i = 0; i < size(); i++) {
                theItems[i] = old[i];//将原来的数组中的值逐个赋值给新数组
            }
        }
    }

    @Override
    public Iterator<String> iterator() {
        return new ArrayListIterator();
    }

    private class ArrayListIterator implements Iterator<String>{

        private int current = 0;

        @Override
        //判断是否还有下一个数据
        public boolean hasNext() {
            return current<size();
        }

        @Override
        //返回下一个数据
        public String next() {
            if(!hasNext()){
                throw new NoSuchElementException();
            }
            return (String) theItems[current++];
        }

        @Override
        //移除刚才返回的下一个数据(next()返回的值)
        public void remove() {
            MyArrayList.this.remove(--current);
        }

    }

}

打印结果:
这里写图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值