This Java code defines a class called Decrease_Capacity with a main method that creates an ArrayList of String called col_list. The ensureCapacity method is used to set the initial capacity of the ArrayList to 20. The add method is then used to add several String elements to the list.
The trimToSize method is then called on the col_list to decrease the capacity of the ArrayList to the current number of elements in the list. This can be useful to reduce the memory usage of the ArrayList if it has been over-allocated.
import java.util.ArrayList; public class Decrease_Capacity { public static void main(String[] args) { ArrayList<String> col_list = new ArrayList<String>(); col_list.ensureCapacity(20); col_list.add("Blue"); col_list.add("Green"); col_list.add("Pink"); col_list.add("Black"); col_list.add("Red"); col_list.add("orange"); col_list.add("White"); //System.out.println("Before Reducing size of Numbere : "+col_list.size()); //reducing the current capacity to current size col_list.trimToSize(); //System.out.println("Affter Reducing size of Numbere : "+col_list.size()); } }
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions