This Java program demonstrates how to convert an ArrayList to an array. Here's how the code works:
Note that the toArray() method returns an array of type Object, so if the ArrayList contains elements of a specific type, you will need to cast the elements to that type after converting the ArrayList to an array.
import java.util.ArrayList; public class ArrayList_ToArray { public static void main(String[] args) { ArrayList<String> fru_list = new ArrayList<String>(); fru_list.add("Papaya"); fru_list.add("Mulberry"); fru_list.add("Apple"); fru_list.add("Banana"); fru_list.add("Cherry"); fru_list.add("Watermelon"); System.out.println("ArrayList : "+fru_list); System.out.println("\nConvert an ArrayList to Array .."); Object[] arr = fru_list.toArray(); for (Object obj : arr) { System.out.println(obj); } } }
ArrayList : [Papaya, Mulberry, Apple, Banana, Cherry, Watermelon] Convert an ArrayList to Array .. Papaya Mulberry Apple Banana Cherry Watermelon
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions