gpt4 book ai didi

java - 将多个 Arraylists 合并为一个

转载 作者:太空宇宙 更新时间:2023-11-04 12:24:08 27 4
gpt4 key购买 nike

好吧,我想以这样一种方式合并这些数组列表,使它们相互插入并创建下面的句子。输出:“这是我试图重构的句子”

List<String> list1 = Arrays.asList(This, , , , , , , , reconstruct);
List<String> list2 = Arrays.asList (, is, the, , , , trying, ,);
List<String> list3 = Arrays.asList(, , ,sentence, that, I'm, , to,);

我尝试使用list.addall(list2),但它只是将每个列表放在另一个列表旁边。如何整合该列表?

最佳答案

你可以有像下面这样的东西

    List<String> list1 = Arrays.asList("This", "", "", "", "", "", "", "", " reconstruct");
List<String> list2 = Arrays.asList("", " is", " the", "", "", "", " trying", "", "");
List<String> list3 = Arrays.asList("", "", "", " sentence", " that", " I'm", "", " to", "");

StringBuilder sentense = new StringBuilder();
for (int i = 0; i < list1.size(); i++) {
sentense.append(list1.get(i)).append(list2.get(i)).append(list3.get(i));
}
System.out.print(sentense.toString());

关于java - 将多个 Arraylists <String> 合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38543365/

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