最近公司没什么项目,闲来无事,写了段代码,模拟下ArrayList,代码如下:
package com.test;
public class ImitateArrayList<T> {
//定义一个默认的容器大小
private static final int DEFAULT_CAPACITY = 16;
//存放数据的数组
private Object[] elements;
//元素的数量和指向数组的下一个索引
private int size;
public ImitateArrayList(){
this(DEFAULT_CAPACITY);
}
public ImitateArrayList(int capacity){
if(capacity < 0){
throw new IllegalArgumentException("invalid capacity "+ capacity);
}
elements = new Object[capacity];
}
/*
* 向容器中添加元素
*/
public void addElement(T obj){
if(checkCapacity()){
ensurceCapacity();
}
elements[size++] = obj;
}
/*
* 增加数组的容量
*/
private void ensurceCapacity(){
int oldCapacity = elements.length;
int newCapacity = oldCapacity * 3 / 2;
Object[] oldData = elements;
elements = new Object[newCapacity];
System.arraycopy(oldData,0,elements,0,size);
}
/*
* 返回容器中元素的个数
*/
public int size(){
return size;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder(100);
stringBuilder.append("[");
for(int i = 0; i < size;i++){
if(i == size - 1){
stringBuilder.append(elements[i].toString());
}else{
stringBuilder.append(elements[i].toString() + ",");
}
}
stringBuilder.append("]");
return stringBuilder.toString();
}
public Object remove(int index){
checkIndex(index);
Object oldElement = elements[index];
moveElement(index,Operation.REMOVE);
size--;
return oldElement;
}
//检验索引
private void checkIndex(int index){
if(index >= size || index < 0){
throw new IllegalArgumentException("invalid argument "+index);
}
}
//移动元素
private void moveElement(int index,Operation operation) {
if(operation == Operation.REMOVE ){
for(int i = index; i < size - 1;i++){
elements[i] = elements[i + 1];
}
}else if(operation == Operation.ADD){
for(int i = elements.length - 2; i >= index ;i--){
elements[i + 1] = elements[i];
}
}
}
//获取数组中的一个元素
public T get(int index){
this.checkIndex(index);
return (T)elements[index];
}
//在固定的索引位置插入一个元素
public void add(int index,T element){
//是否要增加容量
if(checkCapacity()){
ensurceCapacity();
}
//检查index
if(index < 0 || index >= elements.length){
throw new IllegalArgumentException("invalid index "+ index);
}
//如果
if(index >= size){
elements[size] = element;
}else{
moveElement(index,Operation.ADD);
elements[index] = element;
}
size++;
}
//检验当前的元素个数是否等于容器的容量
private boolean checkCapacity() {
// TODO Auto-generated method stub
if(size >= elements.length){
return true;
}
return false;
}
//删除传入的某个元素
public boolean remove(Object obj){
boolean isDeleted = false;
if(obj == null){
isDeleted = true;
}else{
int index = this.indexOf(obj);
remove(index);
isDeleted = true;
}
return isDeleted;
}
//得到元素的索引位置
public int indexOf(Object obj){
int index = -1;
for(int i = 0;i < size;i++){
if(obj.equals(elements[i])){
index = i;
}
}
return index;
}
}
//iterator的实现
private class IteratorImpl implements Iterator<T>{
private int cursor = 0;
private int lastRef = -1;
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return cursor < size;
}
@Override
public T next() {
// TODO Auto-generated method stub
T next = ImitateArrayList.this.get(cursor);
lastRef = cursor++;
return next ;
}
@Override
public void remove() {
// TODO Auto-generated method stub
if(lastRef == -1){
throw new IllegalStateException("before remove you must call next() method");
}
ImitateArrayList.this.remove(lastRef);
if(lastRef < cursor){
cursor--;
}
lastRef = -1;
}
}
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return new IteratorImpl();
}
下面是测试代码:
package com.test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class ImitateArrayListTest {
private ImitateArrayList<Object> imitateArrayList;
@Before
public void setUp(){
imitateArrayList = new ImitateArrayList<Object>(2);
}
/*
* 测试向顺序存储列表添加元素
*/
@Test
public void testAddElement() {
imitateArrayList.addElement("abc");
imitateArrayList.addElement("def");
int actual = imitateArrayList.size();
assertEquals(2,actual);
}
/*
* 测试超出容量
*/
@Test
public void testExceedInitCapacity(){
imitateArrayList.addElement("abc");
imitateArrayList.addElement("def");
imitateArrayList.addElement("jkl");
imitateArrayList.addElement("kif");
int actual = imitateArrayList.size();
assertEquals(4,actual);
}
/*
* 测试toString
*/
@Test
public void testToString(){
imitateArrayList.addElement("abc");
imitateArrayList.addElement("def");
imitateArrayList.addElement("jkl");
imitateArrayList.addElement("kif");
String actual = imitateArrayList.toString();
assertEquals("[abc,def,jkl,kif]",actual);
}
/*
* 测试删除一个元素
*/
@Test
public void testRemove(){
imitateArrayList.addElement("abc");
imitateArrayList.addElement("def");
imitateArrayList.remove(0);
Object actual = imitateArrayList.get(0);
assertEquals("def",actual);
}
/*
* 测试在指定的索引位置增加一个元素
*/
@Test
public void testAddSpecifyIndex(){
imitateArrayList.add(0,"abc");
imitateArrayList.add(0,"def");
Object actual = imitateArrayList.get(0);
System.out.print(imitateArrayList.toString());
assertEquals("def",actual);
}
/*
* 测试在指定的索引位置增加一个元素
*/
@Test
public void testRemoveByObject(){
imitateArrayList.add(0,"abc");
imitateArrayList.add(0,"def");
boolean actual = imitateArrayList.remove("abc");
System.out.println(imitateArrayList.get(0));
assertEquals(true,actual);
}
}
只写了arrayList的部分功能