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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.