Java 从不确定的金额中寻找所有可能的组合
给出一份清单<代码>列表如果内部列表可以是任意大小,而外部列表是传递给函数的变量的大小,我需要找到包含元素的所有可能组合 因为我的解释可能很糟糕,这里有一个输入和输出示例 输入:Java 从不确定的金额中寻找所有可能的组合,java,arrays,list,arraylist,multidimensional-array,Java,Arrays,List,Arraylist,Multidimensional Array,给出一份清单列表如果内部列表可以是任意大小,而外部列表是传递给函数的变量的大小,我需要找到包含元素的所有可能组合 因为我的解释可能很糟糕,这里有一个输入和输出示例 输入: [ ['a', 'b'], ['c', 'd', 'e', 'f'], ['g', 'h'], ['i'] ] 输出: [ ['a', 'c', 'g', 'i'], ['a', 'c', 'h', 'i'], ['a', 'd', 'g', 'i'], ['a
[
['a', 'b'],
['c', 'd', 'e', 'f'],
['g', 'h'],
['i']
]
输出:
[
['a', 'c', 'g', 'i'],
['a', 'c', 'h', 'i'],
['a', 'd', 'g', 'i'],
['a', 'd', 'h', 'i'],
['a', 'e', 'g', 'i'],
['a', 'e', 'h', 'i'],
['a', 'f', 'g', 'i'],
['a', 'f', 'h', 'i'],
['b', 'c', 'g', 'i'],
['b', 'c', 'h', 'i'],
['b', 'd', 'g', 'i'],
['b', 'd', 'h', 'i'],
['b', 'e', 'g', 'i'],
['b', 'e', 'h', 'i'],
['b', 'f', 'g', 'i'],
['b', 'f', 'h', 'i']
]
我对如何完成这件事感到十分困惑。我实在是想不起来了,任何帮助都将不胜感激。检查一下,我认为这可以解决您的问题
public class Test {
public static void main(String a[]) {
List<List<String>> lists = Arrays.asList(Arrays.asList("a", "b"), Arrays.asList("c", "d", "e", "f"), Arrays.asList("g", "h"), Arrays.asList("i"));
System.out.println("lists = " + lists);
List<List<String>> out = getOutPut(new ArrayList<>(), lists);
System.out.println("out = " + out);
}
private static List<List<String>> getOutPut(List<String> conList, List<List<String>> lists) {
List<List<String>> out = new ArrayList<>();
if (lists.size() == 1) {
lists.get(0).forEach(s -> {
List<String> newList = new ArrayList<>(conList);
newList.add(s);
out.add(newList);
});
} else {
lists.get(0).forEach(s -> {
List<String> newList = new ArrayList<>(conList);
newList.add(s);
out.addAll(getOutPut(newList, lists.subList(1, lists.size())));
});
}
return out;
}
}
公共类测试{
公共静态void main(字符串a[]{
列表列表=Arrays.asList(Arrays.asList(“a”、“b”)、Arrays.asList(“c”、“d”、“e”、“f”)、Arrays.asList(“g”、“h”)、Arrays.asList(“i”);
System.out.println(“lists=“+lists”);
List out=getOutPut(新的ArrayList(),lists);
System.out.println(“out=“+out”);
}
私有静态列表getOutPut(列表conList,列表List){
List out=new ArrayList();
如果(lists.size()==1){
lists.get(0).forEach(s->{
List newList=newarraylist(conList);
新列表。添加(s);
out.add(newList);
});
}否则{
lists.get(0).forEach(s->{
List newList=newarraylist(conList);
新列表。添加(s);
out.addAll(getOutPut(newList,lists.subList(1,lists.size()));
});
}
返回;
}
}
检查一下,我认为这可以解决您的问题
public class Test {
public static void main(String a[]) {
List<List<String>> lists = Arrays.asList(Arrays.asList("a", "b"), Arrays.asList("c", "d", "e", "f"), Arrays.asList("g", "h"), Arrays.asList("i"));
System.out.println("lists = " + lists);
List<List<String>> out = getOutPut(new ArrayList<>(), lists);
System.out.println("out = " + out);
}
private static List<List<String>> getOutPut(List<String> conList, List<List<String>> lists) {
List<List<String>> out = new ArrayList<>();
if (lists.size() == 1) {
lists.get(0).forEach(s -> {
List<String> newList = new ArrayList<>(conList);
newList.add(s);
out.add(newList);
});
} else {
lists.get(0).forEach(s -> {
List<String> newList = new ArrayList<>(conList);
newList.add(s);
out.addAll(getOutPut(newList, lists.subList(1, lists.size())));
});
}
return out;
}
}
公共类测试{
公共静态void main(字符串a[]{
列表列表=Arrays.asList(Arrays.asList(“a”、“b”)、Arrays.asList(“c”、“d”、“e”、“f”)、Arrays.asList(“g”、“h”)、Arrays.asList(“i”);
System.out.println(“lists=“+lists”);
List out=getOutPut(新的ArrayList(),lists);
System.out.println(“out=“+out”);
}
私有静态列表getOutPut(列表conList,列表List){
List out=new ArrayList();
如果(lists.size()==1){
lists.get(0).forEach(s->{
List newList=newarraylist(conList);
新列表。添加(s);
out.add(newList);
});
}否则{
lists.get(0).forEach(s->{
List newList=newarraylist(conList);
新列表。添加(s);
out.addAll(getOutPut(newList,lists.subList(1,lists.size()));
});
}
返回;
}
}
这是我的实现。它似乎工作得很好。我使用字符串[][]作为输入,但您可以根据需要进行更改
public class Main {
/**
* This method nests (input.length) for loops to build the desired output.
* @param input The input, change the data type as needed
* @param i An index used to keep track of the element we are checking within the nested for loops
* @param output The output, again, change the data type as needed
* @param newElementOutput A String[] used to temporarily hold the elements of each String[] in the output.
*/
private static void runThrough(String[][] input, int i, ArrayList<String[]> output, String[] newElementOutput){
if(i < input.length - 1){
for(String element:input[i]){
newElementOutput[i] = element;
runThrough(input, i+1, output, newElementOutput);
}
}else if(i == input.length - 1){
for(String element:input[i]){
newElementOutput[i] = element;
output.add(Arrays.copyOf(newElementOutput, input.length)); // a copy so when newOutput changes, the array added to output will stay the same since the reference is different
}
}
}
public static void getCombinations(String[][] input, boolean printTest){
String[] newOutput = new String[input.length];
ArrayList<String[]> output = new ArrayList<>();
runThrough(input, 0, output, newOutput);
// Print the results, to check that everything is as it should
if (printTest) printResults(output);
}
private static void printResults(ArrayList<String[]> output){
System.out.println("Combinations:\n");
for(String[] test:output){
for(String combination:test){
System.out.print(combination);
}
System.out.print("\n");
}
/*
Since in the given example there are four sets of 2, 4, 2 and 1 elements, respectively,
there should be 2 * 4 * 2 * 1 = 16 different combinations
*/
System.out.println("\nTotal number of combinations: " + output.size());
}
public static void main(String[] args){
String[][] arr = {
{"a", "b"},
{"c", "d", "e", "f"},
{"g", "h"},
{"i"}};
getCombinations(arr, true);
}
}
公共类主{
/**
*此方法嵌套(input.length)循环以生成所需的输出。
*@param输入输入,根据需要更改数据类型
*@param i用于跟踪我们在嵌套for循环中检查的元素的索引
*@param output输出,再次根据需要更改数据类型
*@param newElementOutput用于临时保存输出中每个字符串[]的元素的字符串[]。
*/
私有静态void运行贯穿(字符串[][]输入,int i,ArrayList输出,字符串[]newElementOutput){
如果(i
这是我的实现。它似乎工作得很好。我使用字符串[][]作为输入,但您可以根据需要进行更改
public class Main {
/**
* This method nests (input.length) for loops to build the desired output.
* @param input The input, change the data type as needed
* @param i An index used to keep track of the element we are checking within the nested for loops
* @param output The output, again, change the data type as needed
* @param newElementOutput A String[] used to temporarily hold the elements of each String[] in the output.
*/
private static void runThrough(String[][] input, int i, ArrayList<String[]> output, String[] newElementOutput){
if(i < input.length - 1){
for(String element:input[i]){
newElementOutput[i] = element;
runThrough(input, i+1, output, newElementOutput);
}
}else if(i == input.length - 1){
for(String element:input[i]){
newElementOutput[i] = element;
output.add(Arrays.copyOf(newElementOutput, input.length)); // a copy so when newOutput changes, the array added to output will stay the same since the reference is different
}
}
}
public static void getCombinations(String[][] input, boolean printTest){
String[] newOutput = new String[input.length];
ArrayList<String[]> output = new ArrayList<>();
runThrough(input, 0, output, newOutput);
// Print the results, to check that everything is as it should
if (printTest) printResults(output);
}
private static void printResults(ArrayList<String[]> output){
System.out.println("Combinations:\n");
for(String[] test:output){
for(String combination:test){
System.out.print(combination);
}
System.out.print("\n");
}
/*
Since in the given example there are four sets of 2, 4, 2 and 1 elements, respectively,
there should be 2 * 4 * 2 * 1 = 16 different combinations
*/
System.out.println("\nTotal number of combinations: " + output.size());
}
public static void main(String[] args){
String[][] arr = {
{"a", "b"},
{"c", "d", "e", "f"},
{"g", "h"},
{"i"}};
getCombinations(arr, true);
}
}
公共类主{
/**
*此方法嵌套(input.length)循环以生成所需的输出。
*@param输入输入,根据需要更改数据类型
*@param i用于跟踪我们在嵌套for循环中检查的元素的索引
*@param output输出,再次根据需要更改数据类型
*@param newElementOutput用于临时保存输出中每个字符串[]的元素的字符串[]。
*/
私有静态void运行贯穿(字符串[][]输入,int i,ArrayList输出,字符串[]newElementOutput){
如果(i