arraylist trimtosize 28 29 method in java

Solutions on MaxInterview for arraylist trimtosize 28 29 method in java by the best coders in the world

showing results for - "arraylist trimtosize 28 29 method in java"
Liah
12 May 2016
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}