interface MyList{
public void add(Object o);
public void add(int index,Object o);
}
abstract class MyAbstractList implements MyList{
protected int size;
public void add(Object o)
{
add(size,o);
size++;
}
}
class MyArrayList extends MyAbstractList{
private Object[] data=new Object[10];
public void add(int index,Object o){
for(int i=size-1;i>=index;i--){
data[i+1]=data[i];
data[index]=o;
size++;
}
}
public String toString(){
StringBuffer result=new StringBuffer("[");
for(int i=0;i<size;i++){
result.append(data[i]);
if(i<size-1)result.append(", ");
}
return result.toString()+"]";
}
}
class Test{
public static void main(String[] args){
MyArrayList list=new MyArrayList();
list.add("tom") ;
System.out.println(list);
}
}
为什么 结果是 [null]
想要的输出是 [tom] 要怎么修改~