Java中的Vector removeElementAt()方法

这个 JAVAutil。矢量 .removeElementAt(整数索引) 方法用于从特定位置或索引中移除向量中的元素。在这个过程中,向量的大小会自动减少一个元素,并且在移除的元素向下移动一个位置后,所有其他元素都会自动减少。

null

语法:

Vector.removeElementAt(int index)

参数: 此方法接受一个强制参数 指数 整数数据类型,指定要从向量中删除的元素的位置。

返回值: 这种方法有很多优点 无效的 返回类型。这意味着它不会返回任何东西。

下面的程序演示了Java。util。矢量。删除(int索引)方法:

// Java code to illustrate removeElementAt()
import java.util.*;
public class VectorDemo {
public static void main(String args[])
{
// Creating an empty Vector
Vector<String> vec_tor = new Vector<String>();
// Use add() method to add elements in the Vector
vec_tor.add( "Geeks" );
vec_tor.add( "for" );
vec_tor.add( "Geeks" );
vec_tor.add( "10" );
vec_tor.add( "20" );
// Output the Vector
System.out.println( "Vector: " + vec_tor);
// Initial size
System.out.println( "The initial size is: " + vec_tor.size());
// Remove the element at 3rd position
vec_tor.removeElementAt( 2 );
// Print the final Vector
System.out.println( "Final Vector: " + vec_tor);
// Final size
System.out.println( "The final size is: " + vec_tor.size());
}
}


输出:

Vector: [Geeks, for, Geeks, 10, 20]
The initial size is: 5
Final Vector: [Geeks, for, 10, 20]
The final size is: 4

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享