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

Java Vector removeRange() Method

Java Vector removeRange() Method

Java Vector Java Vector


removeRange() The method is in Java's Vector A protected method provided by the class, used to remove from the Vector all elements within the specified range. This method is defined in the java.util.Vector class and inherited from java.util.AbstractList class.

Syntax Description


protected void removeRange(int fromIndex, int toIndex)

Parameter Description

Parameter name Type Description
fromIndex int The index of the first element to remove (inclusive)
toIndex int The index after the last element to remove (exclusive)

Method Characteristics

  1. Range Removal: Can remove multiple elements in a contiguous range at once
  2. Index Boundaries:
  3. fromIndex must be less than or equal to toIndex
  4. Both indexes must be within the valid range of the Vector (0 ≤ fromIndex ≤ toIndex ≤ size())
  5. - `fromIndex` must be less than or equal to `toIndex` - Both indexes must be within the valid range of the Vector (0 ≤ fromIndex ≤ toIndex ≤ size())
    1. Performance Impact: After removing elements, the indexes of all subsequent elements shift forward accordingly
    2. Protected Method: Can only be called directly in Vector 's subclasses, or via reflection

    Usage examples

    Example

    import

    java.util.Vector

    ;

    public

    class

    CustomVector

    <
    E
    extends
    Vector
    <
    E
    {
    public
    void
    removeElementsInRange
    (
    int
    from,
    int
    to
    )
    {

    // Can directly call the parent class's removeRange() method

           

    this

    .

    removeRange

    (

    from, to

    )

    ;

       

    }

       

       

    public

    static

    void

    main

    (

    String

    [

    ]

    args

    )

    {

            CustomVector

    <
    String
    vector
    =
    new
    CustomVector
    <>
    (
    )

    ;

           

           

    // Add elements

            vector.

    add

    (

    "A"

    )

    ;

            vector.

    add

    (

    "B"

    )

    ;

            vector.

    add

    (

    "C"

    )

    ;

            vector.

    add

    (

    "D"

    )

    ;

            vector.

    add

    (

    "E"

    )

    ;

           

           

    System

    .

    out

    .

    println

    (

    "Original Vector: "

    vector

    )

    ;

    // Output: [A, B, C, D, E]

           

           

    // Remove elements at indexes 1 to 3 (B and C)

            vector.

    removeElementsInRange

    (

    1

    ,

    3

    )

    ;

           

           

    System

    .

    out

    .

    println

    (

    "Vector after removal: "

    vector

    )

    ;

    // Output: [A, D, E]

       

    }

    }


    Notes

    1. Index Out of Bounds: If the specified index is out of range, throws IndexOutOfBoundsException
    2. Immutable Collection: Cannot be used on immutable collections or Collections.unmodifiableList() wrapped Vectors
    3. Concurrent Modification: Additional synchronization measures are needed when used in a multithreaded environment
    4. Alternatives: If you do not need to inherit from Vector, you can use the subList(fromIndex, toIndex).clear()

    Performance considerations

    removeRange() method's time complexity is O(n), where:

    For frequent insertion and deletion operations, consider using LinkedList may be more efficient.


    Common questions

    1. Why is removeRange() a protected method?

    This is a design decision of the Java collections framework, intended to encourage developers to use the more general List interface methods. If you need to expose this functionality, you can wrap it in a subclass.

    2. How can range removal be implemented without inheriting from Vector?

    You can use the following alternative:

    Example

    Vector

    <
    String
    vector
    =
    new
    Vector
    <>
    (
    )

    ;

    // Add elements...

    // Use a combination of subList and clear

    vector.

    subList

    (

    1

    ,

    3

    )

    .

    clear

    (

    )

    ;

    3. What is the difference between removeRange() and removeAll()?

    Java Vector Java Vector