好的,所以我想以这样的方式合并这些数组列表,使它们相互插入并创建下面的句子。 输出:“这是我正在尝试重建的句子”
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),但它只是将每个列表放在另一个旁边。如何整合列表?
答案 0 :(得分:0)
您可以拥有以下内容
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());
答案 1 :(得分:0)
假设给定列表具有相同的大小。您可以使用与此类似的代码。
for(int i =0; i<list1.size();i++){
if(list1.get(i)==""){
//replace with sentence from other lists
if(list2.get(i)!=""){
list1.add(i,list2.get(i));
}
else if(list3.get(i)!=""){
list1.add(i,list3.get(i));
}
}
}
答案 2 :(得分:0)
以下代码仅在您确定单词的每个位置都是唯一的时才有效:
List<String> myList = new ArrayList<String>();
String txt = "";
for (int i = 0; i < list1.size(); i++) {
txt = list1.get(i) + list2.get(i) + list3.get(i);
myList.add(txt);
}
如果要创建新列表,请执行以下操作:
<h:form id="searchForm">
<h:selectBooleanCheckbox binding="#{searchCheckbox}" value="true" pt:data-toggle="toggle">
<f:ajax event="change" render="searchForm:searchPanel" />
</h:selectBooleanCheckbox>
<h:panelGroup id="searchPanel">
<h:panelGroup rendered="#{searchCheckbox.value}">
aaa
</h:panelGroup>
<h:panelGroup rendered="#{not searchCheckbox.value}">
bbb
</h:panelGroup>
</h:panelGroup>
</h:form>
答案 3 :(得分:0)
你可以试试这个。我希望这可以帮助
List<String> list1 = Arrays.asList("This", "", "", "", "", "", "", "", "reconstruct");
List<String> list2 = Arrays.asList ("", "is", "the", "", "", "", "trying", "","");
List<String> list3 = Arrays.asList("", "", "","sentence", "that", "Im", "", "to","");
List<String> mergedValue = new ArrayList<>();
for(int i = 0; i<list1.size();i++){
if(list1.get(i)!=""){
mergedValue.add(list1.get(i));
}
if(list2.get(i)!=""){
mergedValue.add(list2.get(i));
}
if(list3.get(i)!=""){
mergedValue.add(list3.get(i));
}
}