将ArrayList <customtype>转换为CharSequence [] </customtype>

时间:2013-12-25 06:50:21

标签: java android arraylist charsequence

我有一个自定义ArrayList,并希望将其转换为CharSequence []。

我试过这个

List<String> list = Arrays.asList("foo", "bar", "waa");
CharSequence[] cs = list.toArray(new CharSequence[list.size()]);
System.out.println(Arrays.toString(cs)); // [foo, bar, waa]

但它仅适用于字符串类型。

2 个答案:

答案 0 :(得分:6)

这就是我解决它的方式,

ArrayList<customtype> dir = new ArrayList<customtype>();
ArrayList<String> listing = new ArrayList<String>();
    customtype item;
    for(int i=0;i<dir.size();i++)
    {
        item = dir.get(i);
        listing.add(item.getPath()); 
    //getPath is a method in the customtype class which will return value in string format

      }
final CharSequence[] fol_list = listing.toArray(new CharSequence[listing.size()]);
//fol_list will have all the strings from getPath();

答案 1 :(得分:0)

因此对于其他类型,您需要将每个项目转换为字符串:

ArrayList<CustomType> datas = new ArrayList<CustomType>();
....
ArrayList<String> strs = new ArrayList<String>();
for(CustomType data : datas) {
    strs.add(data.toString())
}

如果CustomType是你自己的类,请确保你有toString()方法的重载