模拟java ArrayList实例源码教程



模拟java ArrayList实例源码教程,ArrayList相关方法有哪些?是如何扩容的?ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处:动态的增加和减少元素;实现了ICollection和IList接口;灵活的设置数组的大小。

现在我们通过模拟ArrayList底层的实现,包括学习相关的api知识,我们可以从根本上了解java ArrayList的工作原理以及相关的方法。

以下是模拟java ArrayList的实例代码:

package cn.bjsxt.myCollection;

import java.util.ArrayList;

/**
* 模拟实现JDK中提供的ArrayList类
* @author 马士兵
*
*/
public class MyArrayList {
/**
* The value is used for object storage.
*/
private Object[] value;

/**
* The size is the number of objects used.
*/
private int size;

public MyArrayList(){//构造器的建立,模仿了api里的ArrayList类的构造器
// value = new Object[16];
this(10);
}
public MyArrayList(int size){//模仿另外一个构造器
if(size<0){
try {
throw new Exception(); //手动抛出一个异常。 讲到异常章节再说,先混个眼熟
} catch (Exception e) {
e.printStackTrace();
}
}
value = new Object[size];
}

public int size(){
return size;
}

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

public void add(Object obj){
value[size] = obj;
size++;
if(size>=value.length){
//装不下了。扩容吧!
int newCapacity = value.length*2;
Object[] newList = new Object[newCapacity];
// System.arraycopy(src, srcPos, dest, destPos, length);

for(int i=0;i<value.length;i++){
newList[i] = value[i];
}


value = newList;
}
}

public Object get(int index){
rangeCheck(index);

return value[index];
}

public int indexOf(Object obj){
if(obj==null){
return -1;
}else{
for(int i=0;i<value.length;i++){
if(obj==value[i]){
return i;
}
}
return -1;
}
}

public int lastIndexOf(Object obj){
if(obj==null){
return -1;
}else{
for(int i=value.length-1;i>=0;i–){
if(obj==value[i]){
return i;
}
}
return -1;
}
}

public Object set(int index, Object object) {
rangeCheck(index);
Object old = value[index];
value[index] = object;
return old;
}

public void rangeCheck(int index){
if(index<0||index>size-1){ //[0,size-1]
try {
throw new Exception(); //手动抛出一个异常。 讲到异常章节再说,先混个眼熟
} catch (Exception e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
MyArrayList list = new MyArrayList(2);
list.add(“aaa”);
list.add(new Human(“高琪”));
list.add(“bbbb”);
list.add(“bbbb”);
list.add(“bbbb”);
list.add(“bbbb”);
ArrayList list2;

Human h = (Human) list.get(1);
System.out.println(h.getName());

// System.out.println(list.get(3));
System.out.println(list.size());

}

}