在add方法中出现异常,本人新手!望指教,受教了!
源代码:package com.javaBoos01;
public class SxtArraylist {
private Object[] elementData;
private int size ;
public int size(){
return size;
}
public SxtArraylist(){
this(10);
}
//数组初始化赋值
public SxtArraylist (int initialCapacity){
if(initialCapacity<0){
try {
throw new Exception ();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
elementData = new Object[initialCapacity];
}
}
//数组的扩容和对象添加
public void add(Object obj){
if(size==elementData.length){
Object[] addObj = new Object[size*2+1];
System.arraycopy(elementData,0,addObj,0,elementData.length);
elementData= addObj;
}
elementData[size++] = obj;
}
public static void main(String[] args) {
SxtArraylist list = new SxtArraylist ();
list.add(22);
}
}
|
|