1import java.util.ArrayList;
2public class TrimToSizeMethodJavaExample
3{
4 public static void main(String[] args)
5 {
6 // initially size of ArrayList is 10
7 ArrayList<Integer> al = new ArrayList<Integer>(10);
8 // adding integers to ArrayList using add() method
9 al.add(1);
10 al.add(3);
11 al.add(5);
12 al.add(9);
13 al.add(11);
14 al.add(14);
15 // trims the size of ArrayList to 6 using trimToSize() method
16 al.trimToSize();
17 System.out.println("The ArrayList elements are: ");
18 for(Integer number : al)
19 {
20 System.out.println("Number: " + number);
21 }
22 }
23}