Calculate the total number of combinations according to the format of the string "1,2,3,4,5,6,7"

package com.jarvis.base.util;

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

public class AssembleUtil {

	/**
	 * An ordered collection of non-repeating Sets.
	 */
	private static Set<String> set = new TreeSet<String>();

	/**
	 *Author:Jack
	 *Time: September 13, 2017 2:53:59 PM
	 *@param sourceStr initialized string source information
	 *@param max each combination is several numbers
	 *@return
	 *Return:String[]
	 *Description: Calculate the total number of combinations according to the format of the string "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: September 13, 2017 2:53:56 PM
	 *@param sourceArray initialized string array information
	 *@param max each combination is several numbers
	 *@return
	 *Return:String[]
	 *Description: Calculate the number of groups according to the string array form { "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: September 13, 2017 3:00:18 PM
	 *@param start
	 *@param sourceList
	 *@param max
	 *@return
	 *Return:Set<String>
	 *Description: Calculate the number of groups
	 */
	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: September 13, 2017 3:04:25 PM
	 *@param args
	 *Return:void
	 *Description: Test method
	 */
	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));
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324821490&siteId=291194637