Home » Java programming language

Java Vector trimToSize() Method with Example

Vector Class trimToSize() method: Here, we are going to learn about the trimToSize() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 20, 2020

Vector Class trimToSize() method

  • trimToSize() method is available in java.util package.
  • trimToSize() method is used to trim the vector object capacity to its current size.
  • trimToSize() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • trimToSize() method does not throw an exception at the time of trimming size.

Syntax:

    public void trimToSize();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void trimToSize() method of Vector 

import java.util.Vector;

public class TrimToSize {
 public static void main(String[] args) {
  // Instantiates a Vector object  with
  // initial capacity of "20"
  Vector < String > v = new Vector < String > (20);

  // By using add() method is to add the
  // elements in this v
  v.add("C");
  v.add("C++");
  v.add("JAVA");

  // Display Initial Vector Capacity
  System.out.println("v: " + v.capacity());

  // By using trimToSize() method is to 
  // trim the size of this vector v

  v.trimToSize();

  // Display Trimmed Size
  System.out.println("v.trimToSize(): " + v.capacity());
 }
}

Output

v: 20
v.trimToSize(): 3


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.