This piece of Java code shows how to convert a List, in this case an ArrayList, to an Array by calling the method toArray() on the List object. The toArray() method returns an array of the same type as the array provided as an argument to the method. The argument can be an empty array as in the example below.
public class Main { public void ListToArray() { List<String> carList = new ArrayList<String>(); carList.add("Dodge"); carList.add("Chevrolet"); carList.add("BMW"); carList.add("Toyota"); String[] carArray = carList.toArray(new String[0]); for (String car : carArray) { System.out.println(car); } } public static void main(String[] args) { new Main().ListToArray(); } }
If you need any urgent assistance on Convert_a_List_(ArrayList)_to_an_Array, kindly email your requirement to us at : info@javagenious.com or Contact-an-Expert. Our experts will try their best to solve your problem.
Keyword Tags: Convert_a_List_(ArrayList)_to_an_Array tutorial,Convert_a_List_(ArrayList)_to_an_Array in java,Concepts of Convert_a_List_(ArrayList)_to_an_Array,Java,J2EE,Interview Questions,Convert_a_List_(ArrayList)_to_an_Array Examples
Most programming languages compile source code directly into machine code, suitable for execution on a particular microprocessor architecture. read more
|