gpt4 book ai didi

java - 将 ArrayList 转换为 CharSequence[]

转载 作者:行者123 更新时间:2023-12-01 08:07:02 24 4
gpt4 key购买 nike

我有一个自定义的 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]

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

最佳答案

我是这样解决的

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();

关于java - 将 ArrayList<customtype> 转换为 CharSequence[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20769904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com