欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

自定义ArrayList,

来源: javaer 分享于  点击 7422 次 点评:87

自定义ArrayList,


ArrayList是通过数组实现的。

实现ArrayList类中的以下功能:

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);
        }

    }

}

打印结果:

相关文章

    暂无相关文章

用户点评