List <E> subList(int fromIndex, int toIndex)
描述 (Description)
subList(int fromIndex,int toIndex)方法用于返回此List的部分在fromIndex(包含)和toIndex(不包括)之间的视图。 返回的List由此List支持,因此返回List中的更改将反映在此List中,反之亦然。
声明 (Declaration)
以下是java.util.Vector.subList()方法的声明
public List<e> subList(int fromIndex,int toIndex)
</e>
参数 (Parameters)
fromIndex - 这是subList的低端点(包括)。
toIndex - 这是subList的高端点(独占)。
返回值 (Return Value)
方法调用返回此List中指定范围的视图。
异常 (Exception)
IndexOutOfBoundsException - 如果端点索引值超出范围,则抛出此异常
IllegalArgumentException - 如果端点索引出现故障,则抛出此异常
例子 (Example)
以下示例显示了java.util.Vector.subList()方法的用法。
package com.iowiki;
import java.util.*;
public class VectorDemo {
public static void main(String[] args) {
// create an empty Vector vec with an initial capacity of 4
Vector<Integer> vec = new Vector<Integer>(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]