2009年7月28日 星期二

Remove a range from java.util.Vector

There is a method removeRange in AbstractList class, somehow it is protected. So, if you want to remove a range from a Vector, use subList(which is come from List) instead:
v.subList( 0, 12 ).clear();
The subList method returns a view(or a snapshot) instead of a copy to manipulates original container. So if you want to do any operation in specific range, use subList.

2 則留言:

  1. v.removeAll(v.subList(0,12));
    這樣呢?

    by overing

    回覆刪除
  2. 應該也是可以吧,我對 Java 不熟 orz

    我猜它 return 一個 reference 的好處是可以做其他 vector 沒提供的操作

    回覆刪除