Java Examples - Convert Collection to Array
The following example demonstrates how to use the list.add() and list.toArray() methods of the Java Util class to convert a collection to an array:
Main.java file
import java.util.*;
public class Main{
public static void main(String[] args){
List<String> list = new ArrayList<String>();
list.add("Vegetable");
list.add("Bird");
list.add("Teach");
list.add("Course");
list.add("www.example.com");
String[] s1 = list.toArray(new String[0]);
for(int i = 0; i < s1.length; ++i){
String contents = s1[i];
System.out.print(contents);
}
}
}
The output of running the above code is:
Examplewww.example.comOther Extensions
Java Examples