How to convert list to array in java?
The best and easiest way to convert a List into an Array in Java is to use the .toArray() method.
Likewise, we can convert back a List to Array using the Arrays.asList() method.
The examples below show how to convert List of String and List of Integers to their Array equivalents.
Convert List to Array of String
import java.util.ArrayList;
import java.util.List;
public class ConvertArrayListToArray {
public static void main(String[] args) {
List<String> itemList = new ArrayList<String>();
itemList.add(""item1"");
itemList.add(""item2"");
itemList.add(""item3"");
String[] itemsArray = new String[itemList.size()];
itemsArray = itemList.toArray(itemsArray);
for(String s : itemsArray)
System.out.println(s);
}
}"
🔗Source: stackoverflow.com
🔗Source: Java Interview Questions and Answers