ArrayList类的实现
来源:互联网 发布:java语法分析器 编辑:程序博客网 时间:2024/06/05 17:00
本次简单介绍ArrayList泛型类的实现,为避免与类库中的类相混,将类名定义为MyArrayList。下面概括需要实现的细节。
MyArrayList将保持基础数组,数组的容量,以及存储在MyArrayList中的当前项数。
MyArrayList将提供一种机制以改变基础数组的容量。通过获得一个新数组,将老数组拷贝到新数组中来改变数组的容量,允许虚拟机回收老数组。
MyArrayList将提供get和set的实现。
MyArrayList将提供基本的例程,如size,isEmpty和clear。它们是典型的单行程序,还提供remove,以及两个不同版本的add。如果数组的大小和容量相同,那么这两个add例程将增加容量。
MyArrayList将提供一个实现Iterator接口的类。这个类将存储迭代序列中的下一项的下标,并提供next、hasNext和remove等方法的实现。MyArrayList的迭代器方法直接返回实现的Iterator接口的该类的新构造的实例。
package com.zs.structures.list.arraylist;import java.util.Iterator;import java.util.NoSuchElementException;/** * ArrayList类的实现 * @author ZS * * @param <AnyType> */public class MyArrayList<AnyType> implements Iterable<AnyType>{ private static final int DEFAULT_CAPACITY = 10; private int theSize; private AnyType[] theItems; public MyArrayList() { clear(); } public void clear() { theSize = 0; ensureCapacity(DEFAULT_CAPACITY); } public int size() { return theSize; } public boolean isEmpty() { return size() == 0; } public void trimToSize() { ensureCapacity(size()); } public AnyType get(int index) { if(index < 0 || index >= size()) { throw new ArrayIndexOutOfBoundsException(); } return theItems[index]; } public AnyType set(int index, AnyType newVal) { if(index < 0 || index >= size()) { throw new ArrayIndexOutOfBoundsException(); } AnyType old = theItems[index]; theItems[index] = newVal; return old; } public void ensureCapacity(int newCapacity) { if(newCapacity < size()) { return; } AnyType[] old = theItems; AnyType[] newItems= (AnyType[])new Object[newCapacity]; for (int i = 0; i < size(); i++) { newItems[i] = old[i]; } theItems = newItems; } public boolean add(AnyType x) { add(size(), x); return true; } public void add(int index, AnyType x) { if(theItems.length == size()) { ensureCapacity(size() * 2 + 1); } for (int i = size(); i > index; i--) { theItems[i] = theItems[i - 1]; } theItems[index] = x; theSize++; } public AnyType remove(int index) { AnyType old = theItems[index]; for (int i = index; i < size() - 1; i++) { theItems[i] = theItems[i + 1]; } theSize --; return old; } @Override public Iterator<AnyType> iterator() { return new ArrayListIterator(); } private class ArrayListIterator implements Iterator<AnyType> { private int current = 0; @Override public boolean hasNext() { return current < size(); } @Override public AnyType next() { if(!hasNext()) { throw new NoSuchElementException(); } return theItems[current++]; } public void remove() { MyArrayList.this.remove(current-- ); } }}
0 0
- ArrayList类的实现
- ArrayList类的实现
- ArrayList类的实现
- 自己实现一个ArrayList,模仿ArrayList类的底层结构
- JavaScript实现的ArrayList类[原创]
- java之List的实现类ArrayList
- Java集合ArrayList实现类的总结
- 模仿ArrayList类的内部实现
- ArrayList的实现原理
- ArrayList的实现
- ArrayList的实现原理
- ArrayList的实现原理
- ArrayList的实现
- ArrayList的实现原理
- ArrayList的实现原理
- ArrayList的实现原理
- ArrayList的实现原理
- 对 arraylist的实现
- Web Sercice基础使用-->天气,号码归属地
- Matlab Online 体验
- git push错误failed to push some refs to的解决
- 谁获得了最高奖学金[结构体数组]
- java学习中遇到的Scanner死循环问题
- ArrayList类的实现
- 经典查找算法 --- B-树
- 贪心——HDU 4572
- innodb undo --update undo log补充2
- MPU6050姿态结算(采用硬件I2C实现数据融合算法)
- Eddy的难题
- SpringBoot开发详解(三)--SpringBoot配置文件YML注意事项
- 判断java源文件名称
- java面向对象思想编程