Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 470
user1000535 Avatar asked Jul 13 '12 05:07

user1000535


1 Answers

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()]);
like image 186
Pramod Kumar Avatar answered Oct 23 '22 07:10

Pramod Kumar