當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.Vector.trimToSize()用法及代碼示例


描述

這個trimToSize()方法用於將此向量的容量修剪為向量的當前大小。如果此向量的容量大於其當前大小,則將容量更改為當前大小。

聲明

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

public void trimToSize()

參數

NA

返回值

NA

異常

NA

示例

下麵的例子展示了 java.util.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<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

相關用法


注:本文由純淨天空篩選整理自 java.util.Vector.trimToSize() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。