subList(int fromIndex,int toIndex)%uA0方法用于返回fromIndex(包括),以及则为toIndex(不包括)列表的部分视图。返回由此List支持的List,因此改变了返回的列表将反映在此列表中,反之亦然。
声明
以下是java.util.Vector.subList()方法的声明
public ListsubList(int fromIndex,int toIndex)
参数
-
fromIndex--这是子列表的低端点(包括)。
-
toIndex--这是子列表的高点(不包括)。
返回值
该方法调用返回指定范围内List的视图。
异常
-
IndexOutOfBoundsException--若终端索引值超出范围,这是将会被抛出。
-
IllegalArgumentException--这被抛出,如果终结点索引是无序的。
例子
下面的例子显示java.util.Vector.subList()方法的使用。
package com.yiibai import java.util.Vector public class VectorDemo { public static void main(String[] args) { // create an empty Vector vec with an initial capacity of 4 Vectorvec = new Vector (8) List sublist=new ArrayList(10) // use add() method to add elements in the vector vec.add(4) vec.add(3) vec.add(2) vec.add(1) vec.add(6) vec.add(7) vec.add(9) vec.add(5) // lets make a sublist sublist=vec.subList(2,6) // let us print the size of the vector System.out.println("Let us print the list: "+sublist) } }
现在编译和运行上面的代码示例,将产生以下结果。
Let us print the list: [2, 1, 6, 7]