Home > Java > Java Tutorial > body text

Java uses the subList() function of the List class to intercept a sublist of a list

WBOY
Release: 2023-07-25 18:44:03
Original
1235 people have browsed it

Java uses the subList() function of the List class to intercept the sublist of the list

In the process of Java programming, we often encounter situations where we need to intercept the sublist of the list. In order to meet this need, Java's List class provides the subList() function, which can quickly intercept a part of the original list and return a new sublist. This article introduces the method of intercepting sublists using the subList() function of the List class, and provides some code examples.

Before using the subList() function of the List class, you first need to understand its syntax and usage. The syntax of the subList() function of the List class is as follows:

subList(int fromIndex, int toIndex)

where fromIndex represents the index of the starting position (inclusive), and toIndex represents the index of the ending position (not included).

The following is a simple sample code that demonstrates how to use the subList() function of the List class to intercept a sublist:

import java.util.ArrayList;
import java.util.List;

public class SubListExample {
    public static void main(String[] args) {
        // 创建一个包含10个元素的列表
        List list = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            list.add(i);
        }
        
        // 截取子列表
        List sublist = list.subList(3, 8);
        
        // 输出子列表的元素
        System.out.println("截取后的子列表:");
        for (Integer num : sublist) {
            System.out.println(num);
        }
    }
}
Copy after login

In the above sample code, a list containing 10 elements is first created. list. Then use the subList() function to intercept the sublist with indexes from 3 to 8 (excluding 8). Finally, all elements of the sublist are output through loop traversal. Run the program and you will get the following output:

Intercepted sublist:
4
5
6
7
8

As you can see, Using the subList() function of the List class can intercept sublists very conveniently. The subList() function returns a view object, which is a reference to the original list, not a newly created list. Therefore, modifications to the sublist will affect the original list and vice versa. This point requires special attention.

In addition, it should be noted that when the index during the interception process goes out of bounds, an IndexOutOfBoundsException exception will be thrown. Therefore, before using the subList() function, you need to ensure that the index value passed in is valid.

To summarize, the subList() function of Java's List class can be used to intercept sublists of lists. By passing in the index of the starting position and the ending position, you can easily obtain elements within the specified range. It should be noted that the intercepted sublist is a reference to the original list, and modifications to the sublist will affect the original list. I hope the code examples provided in this article can help readers better understand and use the subList() function of the List class.

The above is the detailed content of Java uses the subList() function of the List class to intercept a sublist of a list. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!