📜  Java中的向量 insertElementAt() 方法及示例

📅  最后修改于: 2022-05-13 01:55:02.639000             🧑  作者: Mango

Java中的向量 insertElementAt() 方法及示例

存在于Java.util 包中的 Vector 类的insertElementAt() 方法用于在 Vector 的指定索引处插入特定元素。元素和位置都作为参数传递。如果在指定索引处插入一个元素,则所有元素都会向上推 1,因此容量会增加,从而为新元素创建空间。

句法:

Vector.insertElementAt()

参数:该方法接受两个参数:

  • element:需要插入到vector中。
  • 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 vec_tor = new Vector();
 
        // 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);
    }
}


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 vec_tor = new Vector();
 
        // 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: [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 vec_tor = new Vector();
 
        // 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]