ArrayList类中的removeRange(int formIndex, int toIndex)方法移除fromIndex与toIndex间的元素。但是我在Eclipse中调用该方法总是出错
The method removeRange(int, int) from the type ArrayList is not visible
请问该如何解决呢?
The method removeRange(int, int) from the type ArrayList is not visible
请问该如何解决呢?
你写个类继承ArrayList就可以用它了。
public class MyList extends ArrayList {
private int fromIndex, toIndex;
public MyList(){
super();
}
public void removeBound(int beginIndex, int toIndex){
removeRange(fromIndex, toIndex);
}
}
但我使用该方法时,比如:removeBound(2,3)或者removeBound(3,3)
都将0-3的元素全都移除。
这是什么问题???
super.removeRange(beginIndex,toIndex);
}因该加一个super
楼主定义这两个变量的用意是什么?直接这样用就可以了呀:
public void removeBound(int beginIndex, int toIndex){
removeRange(beginIndex, toIndex);
}因为你的方法变量toIndex覆盖了实例变量toIndex,所以它的值来自于调用时传入的值。而beginIndex没有覆盖fromIndex的值,fromIndex的值是默认值0,所以就从0开始删除了。这与removeRange()方法没关系,是你使用不当所致。