Java.util.Vector 类

java.util.Vector.subList() 方法返回 从 fromIndex(含)到 toIndex 之间的部分元素。 (如果 fromIndex 和 toIndex 相等,则返回的 List 为空。)

语法

public List<E> subList(int fromIndex, int toIndex) 

    这里,E是容器维护的元素类型。

    参数

    fromIndex指定subList的低端点(含)。
    toIndex指定subList的高端点(不包括)。

    返回值

    返回此列表中指定范围的元素。

    异常

    • 如果端点索引值超出范围(fromIndex < 0 || toIndex > 大小),则抛出IndexOutOfBoundsException
    • 如果端点索引无序(fromIndex > toIndex),则抛出 IllegalArgumentException

    示例:

    在下面的示例中,java.util.Vector.subList() 方法返回一个视图给定向量的部分。

    import java.util.*;
    
    public class MyClass {
      public static void main(String[] args) {
        //创建向量
        Vector<Integer> MyVector = new Vector<Integer>();
    
        //填充向量
        MyVector.add(10);
        MyVector.add(20);
        MyVector.add(30);
        MyVector.add(40);
        MyVector.add(50);
        MyVector.add(60);
        MyVector.add(70);  
         
        //打印向量
        System.out.println("MyVector contains: "+ MyVector);    
    
        //创建向量的部分视图
        List MyList = MyVector.subList(2, 5);
    
        //打印子列表
        System.out.println("MyList contains: "+ MyList);  
      }
    } 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    上述代码的输出将是:

    MyVector contains: [10, 20, 30, 40, 50, 60, 70]
    MyList contains: [30, 40, 50] 
    • 1