目录

void trimToSize()

描述 (Description)

trimToSize()方法用于将此向量的容量修剪为向量的当前大小。 如果此向量的容量大于其当前大小,则容量将更改为当前大小。

声明 (Declaration)

以下是java.util.Vector.trimToSize()方法的声明

public void trimToSize()

参数 (Parameters)

NA

返回值 (Return Value)

NA

异常 (Exception)

NA

例子 (Example)

以下示例显示了java.util.Vector.trimToSize()方法的用法。

package com.iowiki;
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<Integer>(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
↑回到顶部↑
WIKI教程 @2018