场景描述:
我有个测试同事,要测试一个接口,这个接口有多个参数,而且有的参数取值有多个.
比如参数method,可以取值["a","b","c"],并且个数不确定,即method,可以取值ab,也可是是abc
如何获取排列组合的所有值呢??
之前咱们是求排列组合的取值个数,现在要求取值?
首先我们考虑常规的情况:?
我们有n个盒子,分别放置n个元素?
第一回:我们从n个里面选择一个:有n种可能?
第二回:我们从n-1个里面选择一个:有n-1种可能?
第三回:我们从n-2个里面选择一个:有n-2种可能?
第四回:我们从n-3个里面选择一个:有n-3种可能?
……?
最后我们只有一个可选?
?直接上代码:直接上代码:
/*** * @param base :[a,b,c,d] * @param times * @param remaining : 剩余要选择的个数 * @return */ public static void assemble(List<String> result, StringBuffer buffer, String base[], int times, int remaining, boolean isSort) { if (remaining <= 1) { buffer.append(base[base.length - 1]); if(times==0||times==base.length-remaining+1){ addElementBySort(result, buffer, isSort); } } else { for (int i = 0; i < remaining; i++) { StringBuffer bufferTmp = new StringBuffer(buffer); bufferTmp.append(base[base.length - 1 - i]); if(times==0||times==base.length-remaining+1){ addElementBySort(result, bufferTmp, isSort); } assemble(result, bufferTmp, SystemHWUtil.aheadElement(base, base.length - 1 - i), times, remaining - 1, isSort); } } }
?参数说明:
base | 样本 | [a,b,c,d] |
remaining | 剩余可选择的样本个数 | ? |
times | 组合的个数 | ? |
?
工具类完整代码:
package com.common.util; import com.string.widget.util.ValueWidget; import java.util.ArrayList; import java.util.List; /** * Created by huangweii on 2016/1/23. */ public class AssembleUtil { /*** * @param base :[a,b,c,d] * @param times * @param remaining : 剩余要选择的个数 * @return */ public static void assemble(List<String> result, StringBuffer buffer, String base[], int times, int remaining, boolean isSort) { if (remaining <= 1) { buffer.append(base[base.length - 1]); if(times==0||times==base.length-remaining+1){ addElementBySort(result, buffer, isSort); } } else { for (int i = 0; i < remaining; i++) { StringBuffer bufferTmp = new StringBuffer(buffer); bufferTmp.append(base[base.length - 1 - i]); if(times==0||times==base.length-remaining+1){ addElementBySort(result, bufferTmp, isSort); } assemble(result, bufferTmp, SystemHWUtil.aheadElement(base, base.length - 1 - i), times, remaining - 1, isSort); } } } /*** * @param base * @param times * @param isSort : 是否对"acb"进行排序,<br />排序结果:"abc" * @return */ public static List<String> assemble(String base[], int times, boolean isSort) { // Set<String> result = new HashSet<String>(); List<String> result = new ArrayList<String>(); AssembleUtil.assemble(result, new StringBuffer(), base, times, base.length, true); return result; } public static void addElementBySort(List<String> result, StringBuffer buffer, boolean isSort) { String str = buffer.toString(); if (isSort) { str = ValueWidget.sortStr(str); } if (result.size() == 0 || (!result.contains(str))) { result.add(str); } } /*** * 参数的取值个数,ab和ba算一种 * * @param argCount * @return */ public static int getAssembleSum(int argCount) { int sum = 0; for (int i = 0; i < argCount; i++) { int count = i + 1;//参数组合的个数 sum += (SystemHWUtil.factorial(argCount, count) / SystemHWUtil.arrayArrange(count)); } return sum; } }
?依赖jar包见附件:
参考:
原文:http://hw1287789687.iteye.com/blog/2273588