Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Vector subList() Method

Java Vector subList() Method

Java Vector Java Vector


Vector The class is an important part of the Java collections framework and implements dynamic array functionality.subList() The method is Vector A practical method provided by the class, used to extract Vector a portion of elements from an existing to form a new list view.

Method syntax:


public List subList(int fromIndex, int toIndex)

Parameter Description

Notes

  1. The index values must satisfy 0 ≤ fromIndex ≤ toIndex ≤ size()
  2. If both the fromIndex and toIndex are equal, the returned sublist is empty
  3. argument is negative or out of range, throws IndexOutOfBoundsException

Return Value Characteristics

subList() The method returns a view of the original Vector , not an independent copy. This means:

View Characteristics


Usage examples

Example

import

java.util.Vector

;

import

java.util.List

;

public

class

VectorSubListExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a Vector and add elements

        Vector

<

String

colors

=

new

Vector

<>

(

)

;

        colors.

add

(

"Red"

)

;

        colors.

add

(

"Green"

)

;

        colors.

add

(

"Blue"

)

;

        colors.

add

(

"Yellow"

)

;

        colors.

add

(

"Black"

)

;

       

       

System

.

out

.

println

(

"Original Vector: "

colors

)

;

       

       

// Get the sublist (indexes 1 to 3)

        List

<

String

subColors

=

colors.

subList

(

1

,

4

)

;

       

System

.

out

.

println

(

"Sublist: "

subColors

)

;

       

       

// Modifying the sublist affects the original Vector

        subColors.

set

(

0

,

"Light Green"

)

;

       

System

.

out

.

println

(

"Vector after modifying the sublist: "

colors

)

;

       

       

// Remove an element from the sublist

        subColors.

remove

(

"Yellow"

)

;

       

System

.

out

.

println

(

"Vector after removing the element: "

colors

)

;

   

}

}

Example output

原始 Vector: [Red, Green, Blue, Yellow, Black]
子列表: [Green, Blue, Yellow]
修改子列表后的 Vector: [Red, Light Green, Blue, Yellow, Black]
移除元素后的 Vector: [Red, Light Green, Blue, Black]

Common Application Scenarios

1. Processing Partial Data

When you only need to process Vector a portion of the elements in the, you can use subList() to obtain a view of a specific range.

2. Batch Operations

Performing batch operations on a sublist (such as clear(), addAll() , etc.) is more efficient than operating on individual elements.

3. Range Checking

can conveniently check Vector whether a certain portion of elements in the meet specific conditions.


Cautions and Best Practices

1. Concurrency Issues

Vector is thread-safe, but sublist operations may require additional synchronization.

2. Structural Modification

If the original Vector is structurally modified (such as adding/removing elements), the sublist throws ConcurrentModificationException.

3. Performance considerations

For frequent sublist operations, consider converting the sublist into a new ArrayList: ```java List

independentSubList = new ArrayList<>(vector.subList(from, to)); ```

4. Differences from Arrays.asList()

`subList()` returns a dynamic view, while `Arrays.asList()` returns a fixed-size list.

Summary

Vector 's subList() method is a powerful tool that can efficiently handle partial views of a collection. Understanding its view characteristic is crucial for correct use. In actual development, decide based on specific needs whether to use the sublist view directly or create an independent copy.

Java Vector Java Vector