DESKTOP-EA11HIB_20200907-蒋宇飞
一、总结
1.问题
在 Bag.java 中的 remove 方法中 this.remove( index ); 无法运行,因为 Bag.java 中没有别的 remove 方法可以调用。
@Override
public boolean remove(Object o) {
int index = this.indexOf( o );
if( index != -1 ) {
this.remove( index );
return true ;
}
return false;
}
2.解决
需要在 Bag.java 中增加一个 remove 方法,来执行该怎样删除。
public Object remove( int index ) {
if( index < 0 || index > counter ) {
throw new IndexOutOfBoundsException( "被删除元素的索引只能是 [ 0 , " + counter + "]" );
}
Object old = elements[ index ];
System.arraycopy( elements , index + 1 , elements , index , counter - index );
counter-- ;
return old ;
}
二、感想
下午,窗帘一拉,教室里闷热,秋天了还这么热。
近期评论