當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Vector removeRange()用法及代碼示例


Java中Vector的removeRange()方法用於從Vector對象中刪除指定範圍內的所有元素。它將所有後續元素向左移動。此調用通過(toIndex-fromIndex)元素來縮短Vector,其中toIndex是結束索引,fromIndex是將刪除所有元素的開始索引。 (如果toIndex == fromIndex,則此操作無效)

用法:

removeRange(int fromIndex, int toIndex)

參數:有兩個參數:


  • fromIndex:從中刪除索引元素的起始索引。
  • toIndex:範圍[fromIndex-toIndex)之內,所有元素均被刪除。

返回值:此方法不返回任何值。它僅刪除指定範圍內的所有元素。

異常:如果fromIndex或toIndex超出範圍,則此方法將引發IndexOutOfBoundsException(fromIndex = size()或toIndex> size()或toIndex

示例1:

// Java program to understand 
// about vector.removeRange() function 
  
mport java.util.*; 
  
// sice vector removeRange() method is protected 
// Therefore Vector is inherited 
public class GFG extends Vector<String> { 
  
    // main method 
    public static void main(String[] args) 
    { 
        // creating GFG object 
        GFG v = new GFG(); 
  
        // inserting elements into the vector 
        v.add("Geeks"); 
        v.add("for"); 
        v.add("Geeks"); 
        v.add("Ankit"); 
        v.add("Mishra"); 
        v.add("MNNIT"); 
  
        // printing vector before deleting 
        System.out.println("Vector before calling"
                           + " removeRange(): " + v); 
  
        // calling removeRange() function 
  
        v.removeRange(1, 3); 
        // printing after removeRange() called 
  
        System.out.println("Vector after calling"
                           + " removeRange(1, 3): " + v); 
    } 
}
//sice向量removeRange()方法受保護
//因此,Vector是繼承的
輸出:
Vector before calling removeRange(): [Geeks, for, Geeks, Ankit, Mishra, MNNIT]
Vector after calling removeRange(1, 3): [Geeks, Ankit, Mishra, MNNIT]

示例2:

// Java program to understand 
// about vector.removeRange() function 
  
import java.util.*; 
  
// sice vector removeRange() method is protected 
// Therefore Vector is inherited 
public class GFG extends Vector<String> { 
  
    // main method 
    public static void main(String[] args) 
    { 
        // creating GFG object 
        GFG v = new GFG(); 
  
        // inserting elements into the vector 
        v.add("Geeks"); 
        v.add("for"); 
        v.add("Geeks"); 
        v.add("Ankit"); 
        v.add("Mishra"); 
        v.add("MNNIT"); 
  
        // printing vector before deleting 
        System.out.println("Vector before calling "
                           + "removeRange(): "
                           + v); 
  
        // calling removeRange() function 
  
        try { 
            // It will generate Runtime Error 
            v.removeRange(1, 16); 
            // printing after removeRange() called 
  
            System.out.println("Vector after "
                               + "calling removeRange(1, 3): "
                               + v); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
輸出:
Vector before calling removeRange(): [Geeks, for, Geeks, Ankit, Mishra, MNNIT]
java.lang.ArrayIndexOutOfBoundsException

注意:強烈建議,如果我們想在任何類中調用removeRange()方法,則該類必須直接或間接擴展Vector類,否則會得到編譯錯誤:方法removeRange()具有受保護的訪問。



相關用法


注:本文由純淨天空篩選整理自ankit15697大神的英文原創作品 Vector removeRange() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。