Answers for "ArrayList trim to size method\\"

1

ArrayList trimToSize() method in java

import java.util.ArrayList;
public class TrimToSizeMethodJavaExample
{
   public static void main(String[] args)
   {
      // initially size of ArrayList is 10
      ArrayList<Integer> al = new ArrayList<Integer>(10);
      // adding integers to ArrayList using add() method
      al.add(1);
      al.add(3);
      al.add(5);
      al.add(9);
      al.add(11);
      al.add(14);
      // trims the size of ArrayList to 6 using trimToSize() method
      al.trimToSize();
      System.out.println("The ArrayList elements are: ");
      for(Integer number : al)
      {
         System.out.println("Number: " + number);
      }
   }
}
Posted by: Guest on October-30-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language