Java中的矢量removeRange()方法与示例
Java中Vector的removeRange()方法用于从Vector对象中移除指定范围内的所有元素。它将任何后续元素向左移动。此调用将 Vector by (toIndex-fromIndex) 元素缩短,其中 toIndex 是结束索引,fromIndex 是要删除所有元素的起始索引。 (如果toIndex==fromIndex,这个操作没有效果)
句法:
removeRange(int fromIndex, int toIndex)
参数:有两个参数:
- fromIndex:要从中删除索引元素的起始索引。
- toIndex:在 range[fromIndex-toIndex) 内,所有元素都被删除。
返回值:此方法不返回任何值。它只删除指定范围内的所有元素。
异常:如果 fromIndex 或 toIndex 超出范围(fromIndex = size() 或 toIndex > size() 或 toIndex < fromIndex),此方法将抛出IndexOutOfBoundsException
示例 1 :
Java
// 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 {
// 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);
}
}
Java
// 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 {
// 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);
}
}
}
// sice 向量 removeRange() 方法受保护
// 因此 Vector 被继承
输出:
// 因此 Vector 被继承
输出:
Vector before calling removeRange(): [Geeks, for, Geeks, Ankit, Mishra, MNNIT]
Vector after calling removeRange(1, 3): [Geeks, Ankit, Mishra, MNNIT]
示例 2 :
Java
// 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 {
// 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() 具有受保护的访问权限。