Online Java Compiler By
JavaTpoint.com
import java.util.*; public class VectorTrimToSizeExample2 { public static void main(String arg[]) { //Create an empty vector Vector < String > colors = new Vector < String > (9); //Add elements in the vector colors.add("White"); colors.add("Green"); colors.add("Black"); colors.add("Pink"); //Prints size of the vector System.out.println("The initial size of the vector: "+colors.capacity()); //Trims the size to the number of elements colors.trimToSize(); System.out.println("The elements of a vector are:"); //Displaying all the elements for (String strcolor : colors) { System.out.println(" " + strcolor); } //Prints size of the vector System.out.println("Size of the vector after trimToSize(): " +colors.capacity()); } }
Output