Online Java Compiler By
JavaTpoint.com
import java.util.*; public class VectorTrimToSizeExample1 { public static void main(String arg[]) { //Create an empty vector Vector
vec = new Vector<>(8); //Add elements in the vector vec.add(1); vec.add(2); vec.add(3); vec.add(4); vec.add(5); //Trims the size to the number of elements vec.trimToSize(); System.out.println("The elements of a vector are:"); //Displaying all the elements for (Integer number : vec) { System.out.println("Number = " + number); } } }
Output