Java Vector elementAt() 方法



描述

Java Vector elementAt(int index) 方法用於獲取向量中指定索引/位置處的元件。

宣告

以下是 java.util.Vector.elementAt() 方法的宣告

public Object elementAt(int index)

引數

index − 這是此向量的索引。

返回值

它返回指定索引處的元件。

異常

ArrayIndexOutOfBoundsException − 如果索引為負數或不小於此 Vector 物件的當前大小。

透過整數向量的索引獲取元素示例

以下示例演示了 Java Vector elementAt() 方法的使用,用於獲取此向量中特定位置/索引處的元素。我們建立了一個 Integer 向量,並使用 add() 方法向其中添加了一些元素。然後,我們使用 elementAt() 方法列印索引 1 處的元素。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty Vector vec with an initial capacity of 4      
      Vector<Integer> vec = new Vector<>(4);

      // use add() method to add elements in the vector
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);

      // let us print the element at 1st position in the vector
      System.out.println("Element at 1st position: "+vec.elementAt(1));
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Element at 1st position : 3

透過字串向量的索引獲取元素示例

以下示例演示了 Java Vector elementAt() 方法的使用,用於獲取此向量中特定位置/索引處的元素。我們建立了一個 String 向量,並使用 add() 方法向其中添加了一些元素。然後,我們使用 elementAt() 方法列印索引 1 處的元素。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty Vector vec with an initial capacity of 4      
      Vector<String> vec = new Vector<>(4);

      // use add() method to add elements in the vector
      vec.add("D");
      vec.add("C");
      vec.add("B");
      vec.add("A");

      // let us print the element at 1st position in the vector
      System.out.println("Element at 1st position: "+vec.elementAt(1));
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Element at 1st position: C

透過物件向量的索引獲取元素示例

以下示例演示了 Java Vector elementAt() 方法的使用,用於獲取此向量中特定位置/索引處的元素。我們建立了一個 Integer 向量,並使用 add() 方法向其中添加了一些元素。然後,我們使用 elementAt() 方法列印索引 1 處的元素。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty Vector vec with an initial capacity of 4      
      Vector<Student> vec = new Vector<>(4);

      // use add() method to add elements in the vector
      vec.add(new Student(1, "Julie"));
      vec.add(new Student(2, "Robert"));
      vec.add(new Student(3, "Adam"));
      vec.add(new Student(4, "Jane"));	  

      // let us print the element at 1st position in the vector
      System.out.println("Element at 1st position: "+vec.elementAt(1));
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Element at 1st position: [ 2, Robert ]
java_util_vector.htm
廣告
© . All rights reserved.