Home > Java > javaTutorial > body text

Get a sublist of a vector in Java using the subList() method of the Vector class

PHPz
Release: 2023-07-24 21:25:08
Original
1591 people have browsed it

Use the subList() method of the Vector class to obtain a sublist of vectors in Java

In Java, the Vector class is a thread-safe dynamic array that implements the List interface. The Vector class provides many methods for manipulating and managing arrays, including the subList() method, which can be used to obtain a sublist of a Vector object.

The definition of the subList() method is as follows:

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

This method accepts two parameters, namely the starting index (fromIndex) and the ending index (toIndex), and returns a list containing the starting index. A sublist of elements between the start index and the end index (excluding the end index).

The following is a sample code that uses the subList() method to obtain a sublist of Vector objects:

import java.util.Vector;
import java.util.List;

public class VectorSubListExample {
    public static void main(String[] args) {
        // 创建一个Vector对象
        Vector<String> vector = new Vector<>();
        vector.add("A");
        vector.add("B");
        vector.add("C");
        vector.add("D");
        vector.add("E");

        // 获取子列表
        List<String> subList = vector.subList(1, 4);

        // 输出子列表元素
        for (String element : subList) {
            System.out.println(element);
        }
    }
}
Copy after login

Run the above code, the output results are as follows:

B
C
D
Copy after login

In the above example, We first created a Vector object and added some elements to it. We then obtained a sublist of Vector objects from index 1 to index 4 using the subList() method. Finally, we output the elements of the sublist using a for-each loop.

It should be noted that the sublist returned by the subList() method is a view of the original Vector object, and operations on the sublist will be directly reflected in the original list. In other words, if we modify an element in the sublist, the corresponding element in the original Vector object will also be modified.

In addition, the sublist obtained through the subList() method is a "half-open interval", that is, it contains the elements corresponding to the starting index, but does not include the elements corresponding to the ending index. So, in our example, the sublist contains elements with indices 1, 2, and 3.

Summary:
This article introduces the operation of using the subList() method of the Vector class to obtain a sublist in Java. The subList() method provides a convenient way to obtain a portion of the contents of a Vector object. By using the start index and end index appropriately, we can flexibly obtain the required sublists and perform related operations.

The above is the detailed content of Get a sublist of a vector in Java using the subList() method of the Vector class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template