insertElementAt()方法的Vector存在於裏麵java.util包用於在 Vector 的指定索引處插入特定元素。元素和位置都作為參數傳遞。如果在指定索引處插入一個元素,則所有元素都會向上推一位,因此容量會增加,為新元素創建空間。
用法:
Vector.insertElementAt()
參數:該方法接受兩個參數:
- element:需要將其插入到載體中。
- index: 指新元素要插入的位置(整數類型)
拋出異常:ArrayIndexOutOfBoundsException如果索引是無效數字。
現在讓我們通過提出字符串元素和整數元素的示例來添加元素,並將我們的方法應用於這兩個元素,隻是為了熟悉此方法在不同原始數據類型上的工作原理。
示例 1:
Java
// Java Program to illustrate insertElementAt()
// Method of Vector class by
// Adding String elements into the Vector
// Importing required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an empty vector of string type
Vector<String> vec_tor = new Vector<String>();
// Adding custom elements into the vector
// using add() method
vec_tor.add("Welcome");
vec_tor.add("To");
vec_tor.add("Geeks");
vec_tor.add("4");
vec_tor.add("Geeks");
// Printing elements of vector
System.out.println("Vector: " + vec_tor);
// Inserting element at 3rd position
// Custom specified
vec_tor.insertElementAt("Hello", 2);
// Inserting element at last position
vec_tor.insertElementAt("World", 6);
// Printing elements of final vector
System.out.println("The final vector is "
+ vec_tor);
}
}
輸出:
Vector: [Welcome, To, Geeks, 4, Geeks] The final vector is [Welcome, To, Hello, Geeks, 4, Geeks, World]
示例 2:
Java
// Java Program to illustrate insertElementAt()
// Method of Vector class by
// Adding Integer Elements into the Vector
// Importing required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an empty Vector of integer type
Vector<Integer> vec_tor = new Vector<Integer>();
// Adding elements into the vector
// using add() method
vec_tor.add(10);
vec_tor.add(20);
vec_tor.add(30);
vec_tor.add(40);
vec_tor.add(50);
// Printing the current elements of vector
System.out.println("Vector: " + vec_tor);
// Inserting element at 1st position
vec_tor.insertElementAt(100, 0);
// Inserting element at 5th position
vec_tor.insertElementAt(200, 4);
// Printing the final elements of Vector
System.out.println("The final vector is "
+ vec_tor);
}
}
輸出:
Vector: [10, 20, 30, 40, 50] The final vector is [100, 10, 20, 30, 200, 40, 50]
相關用法
- Java Vector insertElementAt()用法及代碼示例
- Java Vector indexOf()用法及代碼示例
- Java Vector isEmpty()用法及代碼示例
- Java Vector iterator()用法及代碼示例
- Java Vector replaceAll()用法及代碼示例
- Java Vector sort()用法及代碼示例
- Java Vector spliterator()用法及代碼示例
- Java Vector toString()用法及代碼示例
- Java Vector trimToSize()用法及代碼示例
- Java Vector setElementAt()用法及代碼示例
- Java Vector subList()用法及代碼示例
- Java Vector toArray()用法及代碼示例
- Java Vector retainAll()用法及代碼示例
- Java Vector setSize()用法及代碼示例
- Java Vector add()用法及代碼示例
- Java Vector addAll()用法及代碼示例
- Java Vector addElement()用法及代碼示例
- Java Vector capacity()用法及代碼示例
- Java Vector clear()用法及代碼示例
- Java Vector clone()用法及代碼示例
- Java Vector contains()用法及代碼示例
- Java Vector containsAll()用法及代碼示例
- Java Vector copyInto()用法及代碼示例
- Java Vector elementAt()用法及代碼示例
- Java Vector elements()用法及代碼示例
注:本文由純淨天空篩選整理自kundankumarjha大神的英文原創作品 Vector insertElementAt() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。