📜  Java中的向量subList()方法

📅  最后修改于: 2022-05-13 01:55:00.970000             🧑  作者: Mango

Java中的向量subList()方法

Java.util.Vector.subList()用于返回参数中提到的范围内现有 Vector 的子列表。该方法接受一个上限和一个下限,并返回范围内提到的所有元素。如果元素存在于列表中,则包括下限并且排除上限。基本上,它需要子列表大于等于下限并且严格小于上限元素。

句法:

Vector.subList(int low_index, int up_index)

参数:该方法接受 2 个强制参数:

  • low_index:这是整数类型,定义了评估子列表的起始元素的下限或索引。该元素包含在子列表中。
  • up_index:这是整数类型,定义了对子列表进行评估的结束元素的上限或索引。此元素从子列表中排除

返回值:该方法返回给定参数范围内提到的 Vector 类型的子列表

示例 1:

Java
// Java Program to illustrate subList() method
// of Vector class
 
// Importing required class
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an empty Vector by declaring
        // object of Vector class of Integer type
        Vector vec_tor = new Vector();
 
        // Adding custom input elements
        // using add() method
        vec_tor.add(5);
        vec_tor.add(1);
        vec_tor.add(50);
        vec_tor.add(10);
        vec_tor.add(20);
        vec_tor.add(6);
        vec_tor.add(20);
        vec_tor.add(18);
        vec_tor.add(9);
        vec_tor.add(30);
 
        // Print and display all elements present in vector
        System.out.println("The Vector is: " + vec_tor);
 
        // Creating the sublist vector
        List sub_list = new ArrayList();
 
        // Limiting the values till 5
        // using subList() method via passing arguments
        sub_list = vec_tor.subList(2, 5);
 
        // Displaying the elements present inside list
        System.out.println("The resultant values "
                           + "within the sub list: "
                           + sub_list);
    }
}


Java
// Java Program to illustrate subList() method
// of Vector class
 
// Importing required class
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an empty Vector by
        // creating string object of Vector class
        Vector vec_tor = new Vector();
 
        // Adding custom input elements to above vector
        // object using add() method
        vec_tor.add("Welcome");
        vec_tor.add("To");
        vec_tor.add("Geek");
        vec_tor.add("For");
        vec_tor.add("Geeks");
 
        // Display message only
        System.out.println("The Vector is: " + vec_tor);
 
        // Creating the sublist vector
        List sub_list = new ArrayList();
 
        // Limiting the values till 5
        // using subList() method
        sub_list = vec_tor.subList(1, 5);
 
        // Display elements of List object
        System.out.println("The resultant values "
                           + "within the sub list: "
                           + sub_list);
    }
}


输出:
The Vector is: [5, 1, 50, 10, 20, 6, 20, 18, 9, 30]
The resultant values within the sub list: [50, 10, 20]

示例 2:

Java

// Java Program to illustrate subList() method
// of Vector class
 
// Importing required class
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an empty Vector by
        // creating string object of Vector class
        Vector vec_tor = new Vector();
 
        // Adding custom input elements to above vector
        // object using add() method
        vec_tor.add("Welcome");
        vec_tor.add("To");
        vec_tor.add("Geek");
        vec_tor.add("For");
        vec_tor.add("Geeks");
 
        // Display message only
        System.out.println("The Vector is: " + vec_tor);
 
        // Creating the sublist vector
        List sub_list = new ArrayList();
 
        // Limiting the values till 5
        // using subList() method
        sub_list = vec_tor.subList(1, 5);
 
        // Display elements of List object
        System.out.println("The resultant values "
                           + "within the sub list: "
                           + sub_list);
    }
}
输出
The Vector is: [Welcome, To, Geek, For, Geeks]
The resultant values within the sub list: [To, Geek, For, Geeks]