How to convert ArrayList to String[] in java, Arraylist contains VO objects

user1000535 picture user1000535 · Jul 13, 2012 · Viewed 61.6k times · Source

Please help me to convert ArrayList to String[]. The ArrayList contains values of type Object(VO).

For example,

The problem is that I need to convert a country List to String Array, sort it and then put it in a list. However I am getting a ClassCastException.

Answer

Pramod Kumar picture Pramod Kumar · Jul 13, 2012
String [] countriesArray = countryList.toArray(new String[countryList.size()]);

I have assumed that your country List name is countryList.

So to convert ArrayList of any class into array use following code. Convert T into the class whose arrays you want to create.

List<T> list = new ArrayList<T>();    
T [] countries = list.toArray(new T[list.size()]);