如何从java中的列表中删除元素 [英] How do I remove elements from list in java
问题描述
import java.util。*;
公共类lISTSS {
public static< t> void getCombination(List< t> ... lists){
if(lists == null)return;
getCombinations(new ArrayList< t>(),0, ()b $ b $ / b $ b private static< t> void getCombinations(List< t> soFar,int i,List< t> ... lists){
if(i == lists.length){//没有剩下的清单:
System.out.println(soFar);
}其他{
for(T t:lists [i]){
soFar.add(t); //猜项目
getCombinations(soFar,i + 1,列表); //对其余列表进行递归
soFar.remove(soFar.size() - 1); //清理
}
}
}
public static void main (String args []){
List< string> A = Arrays.asList(one,two,three);
List< string> B = Arrays.asList(四,五);
getCombination(A,B);
}
}
我尝试过:
这是当前的输出:
[一,四]
[一,五]
[两个,四个]
[二,五]
[三,四]
[三,五]
我有上面的代码获取两个列表的所有组合,我得到了输出,但我想删除组合[一,五]和[二,四]。我试过以下没有用。
if(soFar.get(i).equals(one,five)){
soFar.remove(一,五);
}
import java.util.*;
public class lISTSS {
public static <t> void getCombination(List<t>... lists) {
if (lists == null) return;
getCombinations(new ArrayList<t>(), 0, lists);
}
private static <t> void getCombinations(List<t> soFar, int i, List<t>... lists) {
if (i == lists.length) { //no more lists left:
System.out.println(soFar);
} else {
for (T t : lists[i]) {
soFar.add(t); //"guess" item
getCombinations(soFar, i + 1, lists); //recurse on rest of lists
soFar.remove(soFar.size() - 1); //cleanup
}
}
}
public static void main(String args[]) {
List<string> A = Arrays.asList("one","two","three");
List<string> B = Arrays.asList("four","five");
getCombination(A,B);
}
}
What I have tried:
This is the current output:
[one, four]
[one, five]
[two, four]
[two, five]
[three, four]
[three, five]
I have the above code that gets all combinations of two lists, and I got the output, but I want to remove the combinations [one, five] and [two, four]. I've tried the following without avail.
if(soFar.get(i).equals("one, five")) {
soFar.remove("one, five");
}
推荐答案
你需要使用Iterator并调用remove()on迭代器而不是使用for循环。
Iterator(Java Platform SE 7) [ ^ ]
You need to use Iterator and call remove() on iterator instead of using for loop.
Iterator (Java Platform SE 7 )[^]
这篇关于如何从java中的列表中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!