जावा में ArrayList से सभी तत्वों को हटाने के लिए, आइए पहले कुछ तत्वों के साथ एक ArrayList बनाएं -
ArrayList<Integer> arrlist = new ArrayList<Integer>(5); arrlist.add(25); arrlist.add(50); arrlist.add(75); arrlist.add(100); arrlist.add(150); arrlist.add(200); arrlist.add(250);
अब, सभी तत्वों को हटा दें -
arrlist.clear();
उदाहरण
आइए अब पूरा कोड देखें -
import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList<Integer> arrlist = new ArrayList<Integer>(5); arrlist.add(25); arrlist.add(50); arrlist.add(75); arrlist.add(100); arrlist.add(150); arrlist.add(200); arrlist.add(250); for (Integer number : arrlist) { System.out.println("Number = " + number); } int retval = arrlist.size(); System.out.println("List consists of "+ retval +" elements"); System.out.println("Performing clear operation!!"); arrlist.clear(); retval = arrlist.size(); System.out.println("Now, list consists of "+ retval +" elements"); } }
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
Number = 25 Number = 50 Number = 75 Number = 100 Number = 150 Number = 200 Number = 250 List consists of 7 elements Performing clear operation!! Now, list consists of 0 elements