Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");
}
Posted
Updated 4-Apr-16 19:55pm

1 solution

You need to use Iterator and call remove() on iterator instead of using for loop.

Iterator (Java Platform SE 7 )[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900