package com.jarvis.base.util; import java.util.Arrays; import java.util.Set; import java.util.TreeSet; public class AssembleUtil { /** * 有序的不重复的Set集合。 */ private static Set<String> set = new TreeSet<String>(); /** *Author:Jack *Time:2017年9月13日下午2:53:59 *@param sourceStr 初始化的字符串源信息 *@param max 每种组合是几个数字 *@return *Return:String[] *Description:根据字符串“1,2,3,4,5,6,7”的格式计算出来一共有多少组组合 */ public static String[] getAssemble(String sourceStr, int max) { String sourceList [] = sourceStr.split(","); String[] array = getAssemble(sourceList, max); return array; } /** *Author:Jack *Time:2017年9月13日下午2:53:56 *@param sourceArray 初始化的字符串数组信息 *@param max 每种组合是几个数字 *@return *Return:String[] *Description: 根据字符串数组形式来计算一共有多少种组数{ "1", "2", "3", "4", "5","6","7" } */ public static String[] getAssemble(String[] sourceArray, int max) { for (int start = 0; start < sourceArray.length; start++) { doSet(sourceArray[start], sourceArray, max); } String[] arr = new String[set.size()]; String[] array = set.toArray(arr); set.clear(); return array; } /** *Author:Jack *Time:2017年9月13日下午3:00:18 *@param start *@param sourceList *@param max *@return *Return:Set<String> *Description:计算组数 */ private static Set<String> doSet(String start, String[] sourceList, int max) { String[] olds = start.split("_"); if (olds.length == max) { set.add(start.replaceAll("_", "").trim()); } else { for (int s = 0; s < sourceList.length; s++) { if (Arrays.asList(olds).contains(sourceList[s])) { continue; } else { doSet(start + "_" + sourceList[s], sourceList, max); } } } return set; } /** *Author:Jack *Time:2017年9月13日下午3:04:25 *@param args *Return:void *Description:测试方法 */ public static void main(String[] args) { String[] sourceArr = new String[] { "1", "2", "3", "4", "5","6","7" }; String[] resultArr = getAssemble(sourceArr, 3); System.out.println("累计组合:"+resultArr.length+","+Arrays.toString(resultArr)); String sourceStr = "1,2,3,4,5,6,7"; String[] resultArr2 = getAssemble(sourceStr, 3); System.out.println("累计组合:"+resultArr2.length+","+Arrays.toString(resultArr2)); } }
最近下载更多
最近浏览更多
暂无贡献等级
Wsy2860 LV1
2023年9月6日
飘逸的云 LV1
2023年7月6日
我擦嘞
2023年7月6日
暂无贡献等级
wuziayng1232 LV10
2023年5月31日
wangyao112233 LV2
2023年2月1日
二进制2 LV3
2023年1月6日
stonemachen LV3
2022年10月31日
香菇肉饼汤 LV8
2022年10月30日
heiren2
2022年10月28日
暂无贡献等级