Java Vector trimToSize() 方法



描述

Java Vector trimToSize() 方法用於將此向量的容量調整為向量的當前大小。如果此向量的容量大於其當前大小,則容量將更改為當前大小。

宣告

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

public void trimToSize()

引數

返回值

異常

將整數向量調整為指定大小的示例

以下示例演示了 Java Vector trimToSize() 方法的用法。我們使用 add() 方法為 Vector 物件新增幾個整數,並列印向量的容量。然後,我們使用 trimToSize() 調整向量的大小,並再次列印其容量以反映更改。

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<>(10);

      // 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 size of the vector
      System.out.println("Size of the vector: "+vec.capacity());

      // trim the size of the vector
      System.out.println("Trimming the vector");
      vec.trimToSize();

      System.out.println("Size of the vector: "+vec.capacity());
   }     
}

輸出

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

Size of the vector: 10
Trimming the vector
Size of the vector: 4

將字串向量調整為指定大小的示例

以下示例演示了 Java Vector trimToSize() 方法的用法。我們使用 add() 方法為 Vector 物件新增幾個字串,並列印向量的容量。然後,我們使用 trimToSize() 調整向量的大小,並再次列印其容量以反映更改。

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<>(10);

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

      // let us print the size of the vector
      System.out.println("Size of the vector: "+vec.capacity());

      // trim the size of the vector
      System.out.println("Trimming the vector");
      vec.trimToSize();

      System.out.println("Size of the vector: "+vec.capacity());
   }     
}

輸出

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

Size of the vector: 10
Trimming the vector
Size of the vector: 3

將物件向量調整為指定大小的示例

以下示例演示了 Java Vector trimToSize() 方法的用法。我們使用 add() 方法為 Vector 物件新增幾個 Student 物件,並列印向量的容量。然後,我們使用 trimToSize() 調整向量的大小,並再次列印其容量以反映更改。

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<>(10);

      // 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"));

      // let us print the size of the vector
      System.out.println("Size of the vector: "+vec.capacity());

      // trim the size of the vector
      System.out.println("Trimming the vector");
      vec.trimToSize();

      System.out.println("Size of the vector: "+vec.capacity());
   }     
}
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 + " ]";
   }
}

輸出

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

Size of the vector: 10
Trimming the vector
Size of the vector: 3
java_util_vector.htm
廣告