本帖最后由 陈永波 于 2012-7-1 22:04 编辑
import java.util.List;
public class MyArraylistDemo {
private Object[] arr = new Object[10];
private int size;
public void add(Object obj){
if(size==arr.length){
Object[] newArr = new Object[arr.length*3/2 +1];
System.arraycopy(arr, 0, newArr, 0, size);
arr = newArr;
}
arr[size++] = obj;
}
public void add(int index, Object obj){
if(size==arr.length){
Object[] newArr = new Object[arr.length*3/2 +1];
System.arraycopy(arr, 0, newArr, 0, size);
arr = newArr;
}
System.arraycopy(arr, index, arr, index + 1, size-index);
arr[index] = obj;
size++;
}
public void remove(int index){
}
public void remove(Object obj){
}
public void set(int index, Object obj){
if(index >= size){
throw new ArrayIndexOutOfBoundsException("Index" + index + ", size" + size);
}
arr[index] = obj;
}
public Object get(int index){
if(index >= size){
throw new ArrayIndexOutOfBoundsException("Index" + index + ", size" + size);
}
return arr[index];
}
public void addAll(List list){
for(int i = 0; i < list.size(); i++){
add(list.get(i));
if(size==arr.length){
Object[] newArr = new Object[arr.length*3/2 +1];
System.arraycopy(arr, 0, newArr, 0, size);
arr = newArr;
}
}
}
public int size(){
return size;
}
public void clear(){
}
}
求 public void remove(int index){}
public void remove(Object obj){}健壮代码 自己没什么思路了 |
|