package com.test.test.order.tools; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; /** * 将字符数组排列组合,生成list返回 */ public class Arrange { private List<String> strList; public Arrange(String[] strArr){ strList = new ArrayList<>(); strList.addAll(Arrays.asList(strArr)); Iterator<String> strListIter = strList.iterator(); while (strListIter.hasNext()) { String str = strListIter.next(); if (StringUtils.isEmpty(StringUtils.replaceSpace(str))) strListIter.remove(); } } public List<String> getStrList(){ return strList; } public void invok(String[] strArr, int start, int len){ if(start == len-1){ String str = ""; for(int i=0; i<strArr.length; ++i) str+=strArr[i]; if (!strList.contains(str)) strList.add(str); return; } for(int i=start; i<len; i++){ String temp = strArr[start]; strArr[start] = strArr[i]; strArr[i] = temp; invok(strArr, start+1, len); temp = strArr[start]; strArr[start] = strArr[i]; strArr[i] = temp; } } /*public static void main(String[] args) { String key = "北京 中国 建设银行"; String[] strArr = key.split(" "); Arrange a = new Arrange(strArr); a.invok(strArr, 0, strArr.length); System.out.println(a.getStrList()); }*/ }
java - 字符串数组排列组合并返回list
最新推荐文章于 2024-08-21 15:17:10 发布