
- Java.util 包類
- Java.util - 首頁
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包擴充套件
- Java.util - 介面
- Java.util - 異常
- Java.util - 列舉
- Java.util 有用資源
- Java.util - 有用資源
- Java.util - 討論
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
廣告