Java Vector size() 方法



描述

Java Vector size() 方法用於返回此向量中元件的數量。

宣告

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

public int size()

引數

返回值

方法呼叫返回此向量中元件的數量。

異常

獲取整數向量大小的示例

以下示例演示了 Java Vector size() 方法的用法。我們使用 add() 方法為每個元素向 Vector 物件新增幾個整數。使用 size() 方法列印向量的尺寸。並使用 remove(index) 方法移除一個元素,然後再次列印向量的尺寸。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty array list 
      Vector<Integer> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add(20);
      vector.add(30);
      vector.add(20);
      vector.add(30);
      vector.add(15);
      vector.add(22);
      vector.add(11);

      // let us print the size of the vector again
      System.out.println("Vector Size = " + vector.size());
	  
      // remove an element at index 2
      vector.remove(2);

      // let us print the size of the vector again
      System.out.println("Vector Size = " + vector.size());
   }
}

輸出

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

Vector Size = 7
Vector Size = 6

設定字串向量大小的示例

以下示例演示了 Java Vector size() 方法的用法。我們使用 add() 方法為每個元素向 Vector 物件新增幾個字串。使用 size() 方法列印向量的尺寸。並使用 remove(index) 方法移除一個元素,然後再次列印向量的尺寸。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      Vector<String> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add("Welcome");
      vector.add("To");
      vector.add("Tutorialspoint");

      // let us print the size of the vector again
      System.out.println("Vector Size = " + vector.size());
	  
      // remove an element at index 2
      vector.remove(2);

      // let us print the size of the vector again
      System.out.println("Vector Size = " + vector.size());    
   }
}

輸出

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

Vector Size = 3
Vector Size = 2

設定物件向量大小的示例

以下示例演示了 Java Vector size() 方法的用法。我們使用 add() 方法為每個元素向 Vector 物件新增幾個 Student 物件。使用 size() 方法列印向量的尺寸。並使用 remove(index) 方法移除一個元素,然後再次列印向量的尺寸。

package com.tutorialspoint;

import java.util.Vector;

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

      // create an empty vector
      Vector<Student> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add(new Student(1, "Julie"));
      vector.add(new Student(2, "Robert"));
      vector.add(new Student(3, "Adam"));
	  
      // let us print the size of the vector again
      System.out.println("Vector Size = " + vector.size());
	  
      // remove an element at index 2
      vector.remove(2);

      // let us print the size of the vector again
      System.out.println("Vector Size = " + vector.size());      
   }
}

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 + " ]";
   }
}

輸出

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

Vector Size = 3
Vector Size = 2
java_util_vector.htm
廣告