java中两个数组的排列算法
大家好,我的社区 我需要这个:java中两个数组的排列算法,java,Java,大家好,我的社区 我需要这个: List<String> s1 = Arrays.asList(new String[] {"A0","B0","C0","D0"}); List<String> s2 = Arrays.asList(new String[] {"E0","F0","G0","H0"}); int
List<String> s1 = Arrays.asList(new String[] {"A0","B0","C0","D0"});
List<String> s2 = Arrays.asList(new String[] {"E0","F0","G0","H0"});
int iteration = 3;
List<String> result = permutations(s1,s2,iteration);
//result should contain : "A0E0B0F0C0G0", "A0E0B0F0D0H0",...
//result should not contain : some String begining with E0 in this example
//result should not have duplicate values
//Expected behaviour :
//Pick one of the strings from s1, pick one of the strings from s2.
//Concatenate both. Repeat this process "iteration" times.
我发现:
它很有用,但我不知道如何为我的代码更改它
有什么想法吗?非常感谢,看起来很完美!好正如所料,很难检查你答案的正确性。我有一些数学要做,以检查它-
package stackoverflow;
import java.util.Arrays;
import java.util.List;
//Prints permutation of strings in 2 lists, first it takes string from first list and
//then from second list, so on it repeats this process as the given "iteration" number of times
public class PermutationOfTwoArrays
{
public static void main(String[] args)
{
int iteration=3;
List<String> s1 = Arrays.asList(new String[] {"A0","B0","C0","D0"});
List<String> s2 = Arrays.asList(new String[] {"E0","F0","G0","H0"});
int[] indexes=new int[2*iteration];
printPermutationOfTwoArrays(iteration,s1,s2,0,indexes);
}
private static void printPermutationOfTwoArrays(int iteration, List<String> s1,
List<String> s2,int currentIteration,int[] indexes)
{
if(currentIteration>=3) {
System.out.println(s1.get(indexes[0])+s2.get(indexes[1])+
s1.get(indexes[2])+s2.get(indexes[3])+
s1.get(indexes[4])+s2.get(indexes[5]));
return;
}
for(int j=0;j<s1.size();j++) {
indexes[2*currentIteration]=j;
for(int k=0;k<s2.size();k++) {
indexes[2*currentIteration+1]=k;
printPermutationOfTwoArrays(iteration, s1, s2, currentIteration+1, indexes);
}
}
}
}