import java.util.ArrayList; import java.util.List; public class Example { public static void main(String[] args) { Listlist = new ArrayList<>(); list.add("apple"); list.add("banana"); list.add("orange"); String[] array = list.toArray(new String[list.size()]); for (String fruit : array) { System.out.println(fruit); } } }
import java.util.Arrays; import java.util.List; public class Example { public static void main(String[] args) { ListIn this example, we have a List of Integer objects. We convert this list to an array of Integer objects using the toArray() method. We specify the size of the array that needs to be created using the size() method of the list object. Finally, we iterate through the array and print the element. This example belongs to the standard Java library.list = Arrays.asList(1, 2, 3); Integer[] array = list.toArray(new Integer[list.size()]); for (Integer number : array) { System.out.println(number); } } }