0

カスタム 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]

ただし、文字列型でのみ機能します。

4

2 に答える 2

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();
于 2013-12-25T06:50:20.810 に答える
0

したがって、他のタイプの場合は、各アイテムを文字列に変換する必要があります。

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

CustomType が自分自身のクラスである場合は、 toString() メソッドをオーバーロードしていることを確認してください

于 2013-12-25T07:02:40.233 に答える