First, the code creates an ArrayList called list_str and adds several elements to it using the add() method. Then, the code prints the ArrayList using the System.out.println() method.
Next, the code enters a loop that iterates through the ArrayList using the size() method to determine the number of elements in the ArrayList and the get() method to retrieve each element in turn.
Finally, the code prints each element using the System.out.println() method.
import java.util.ArrayList; public class PrintElements_ArrayList { public static void main(String[] args) { ArrayList<String> list_str= new ArrayList<String>(); list_str.add("Printer"); list_str.add("Derive"); list_str.add("Monitor"); list_str.add("Laptop"); list_str.add("Keyboard"); list_str.add("Mouse"); list_str.add("CPU"); System.out.println("Given Array List : " + list_str); System.out.println("\nPrint using index of an Element .. "); int len = list_str.size(); for (int i = 0; i < len; i++) { System.out.println(list_str.get(i)); } } }
Given Array List : [Printer, Derive, Monitor, Laptop, Keyboard, Mouse, CPU] Print using index of an Element .. Printer Derive Monitor Laptop Keyboard Mouse CPU
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions