This Java program demonstrates how to find the size of an ArrayList, add elements to it, and clear the elements. Here's how the code works:
Note that the size() method returns the number of elements in the ArrayList, which is an integer.
import java.util.ArrayList; public class NumElement_ArrayList { public static void main(String[] args) { ArrayList<String> fru_list = new ArrayList<>(); int s = fru_list.size(); System.out.println("After Creating Size of Array List : " + s); fru_list.add("Papaya"); fru_list.add("Mulberry"); fru_list.add("Apple"); fru_list.add("Banana"); fru_list.add("Cherry"); fru_list.add("Watermelon"); fru_list.add("Mango"); fru_list.add("Pineapple"); s = fru_list.size(); System.out.println("Length of ArrayList After Adding Elements : " + s); fru_list.clear(); s = fru_list.size(); System.out.println("Size of ArrayList After Clearing Elements : " + s); } }
After Creating Size of Array List : 0 Length of ArrayList After Adding Elements : 8 Size of ArrayList After Clearing Elements : 0
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions