请帮助我将 ArrayList 转换为 String[]。 ArrayList 包含 Object(VO) 类型的值。
例如,
问题是我需要将国家列表转换为字符串数组,对其进行排序,然后将其放入列表中。但是我收到了 ClassCastException。
最佳答案
String [] countriesArray = countryList.toArray(new String[countryList.size()]);
我假设您的国家/地区列表名称是 countryList
。
因此,要将任何类的 ArrayList 转换为数组,请使用以下代码。将 T 转换为您要创建其数组的类。
List<T> list = new ArrayList<T>();
T [] countries = list.toArray(new T[list.size()]);
关于java - java中如何将ArrayList转成String[],Arraylist包含VO对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11464706/