Java Vector subList() Method
VectorThe class is an important part of the Java collection framework, implementing dynamic array functionality.subList()The method isVectora practical method provided by the class, used to extract from an existingVectora portion of elements from it, forming a new list view.
Method syntax:
public List<E> subList(int fromIndex, int toIndex)
Parameter Description
fromIndex: The starting index of the sublist (inclusive)toIndex: The ending index of the sublist (exclusive)
Notes
- The index values must satisfy
0 ≤ fromIndex ≤ toIndex ≤ size() - If
fromIndexandtoIndexare equal, the returned sublist is empty - If the parameter is negative or out of range, it will throw
IndexOutOfBoundsException
Return Value Characteristics
subList()The method returns the originalVectora view of it, not an independent copy. This means:
View Characteristics
- Modifications to the sublist directly affect the original
Vector - Original
VectorStructural modifications (such as adding/removing elements) will invalidate the sublist - The sublist supports all list operations
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 sublist (index 1 to 3)
List<String> subColors = colors.subList(1, 4);
System.out.println("Sublist: " + subColors);
// Modifying the sublist will affect the original Vector
subColors.set(0, "Light Green");
System.out.println("Vector after modifying sublist: " + colors);
// Remove elements from the sublist
subColors.remove("Yellow");
System.out.println("Vector after removing elements: " + colors);
}
}
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 sublist (index 1 to 3)
List<String> subColors = colors.subList(1, 4);
System.out.println("Sublist: " + subColors);
// Modifying the sublist will affect the original Vector
subColors.set(0, "Light Green");
System.out.println("Vector after modifying sublist: " + colors);
// Remove elements from the sublist
subColors.remove("Yellow");
System.out.println("Vector after removing elements: " + 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 a portion of elements in a `Vector`, you can use `subList()` to get a view of a specific range.2. Batch Operations
Performing batch operations on sublists (such as `clear()`, `addAll()`, etc.) is more efficient than operating on individual elements.3. Range Checking
You can conveniently check whether a portion of elements in a `Vector` meets specific conditions.Notes and Best Practices
1. Concurrency Issues
`Vector` is thread-safe, but sublist operations may require additional synchronization measures.2. Structural Modification
If the original `Vector` is structurally modified (such as adding/removing elements), the sublist will throw `ConcurrentModificationException`.3. Performance Considerations
For frequent sublist operations, consider converting the sublist to a new `ArrayList`: ```java List4. Difference from Arrays.asList()
`subList()` returns a dynamic view, while `Arrays.asList()` returns a fixed-size list.Summary
VectorofsubList()The method is a powerful tool that can efficiently handle partial views of collections. Understanding its view characteristics is crucial for correct usage. In actual development, you should decide based on specific needs whether to use the sublist view directly or create an independent copy.
Java Vector