这就是我目前所拥有的。
package testproject;
public class Combinations{
public Combinations(){
String str_arr[]={"a","b","c"};
GenCombinations(str_arr);
}
public void GenCombinations(String[] str_arr){
System.out.println("Generating All possible Combinations for the following "+str_arr.length+" strings.");
for(int i=0;i<str_arr.length;i++)
{
System.out.print(str_arr[i]+" ");
}
System.out.println("\n------------------------------------------");
/*COMBINATIONS OF LENGTH ONE*/
for(int i=0;i<str_arr.length;i++)
System.out.println(str_arr[i]);
/*COMBINATIONS OF LENGTH TWO*/
for(int i=0;i<str_arr.length;i++)
{
for(int j=0;j<str_arr.length;j++)
{
System.out.println(str_arr[i]+""+str_arr[j]);
}
}
/*COMBINATIONS OF LENGTH THREE*/
for(int i=0;i<str_arr.length;i++)
{
for(int j=0;j<str_arr.length;j++)
{
for(int k=0;k<str_arr.length;k++)
{
System.out.println(str_arr[i]+""+str_arr[j]+""+str_arr[k]);
}
}
}
}
public static void main(String[] args){
new Combinations();
}
}
任何建议将不胜感激..
最佳答案
如果您只需要两个字符串的组合,那么您的代码基本上可以尽可能高效,但需要注意以下事项:
您需要确保输入数组不包含重复项。
如果输入数组中的字符串长度不尽相同,您可能需要检查输出数组中的重复项。
关于java - 如果字符串是数组,则在两个对中查找字符串组合的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7616989/