字符串数组排列组合成新的字符串

论坛 期权论坛 脚本     
匿名网站用户   2020-12-19 15:21   32   0
package com.mindao.app.vpatient.utils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
/* 版权所有: 广州敏道科技有限公司
/*
/* 功能描述: 字符串处理工具类
/*
/* 创 建 人:李国才
/* 创建时间:2016年8月8日 上午10:35:35  
 **/
public class StringUtils {
 /**
  * ABCD等多个数组元素任意组合得到所有的使用了全部元素的新字符串数组,
  * 比如ABC应该有3*2=6种,ABCD 有4*3*2=12种组合
  * @param list
  * @param s
  * @return
  */
 public static List<String> getAllSortList(List<String> list,String s){
  if (list.size()==1){
   List<String> tmpList =new ArrayList<>();
   String tmps= s+list.get(0);
   tmpList.add(tmps);
   return tmpList;
  }
  List<String> allList =new ArrayList<>();
  for (int i=0;i<list.size();i++) {
   List<String> tmpList =new ArrayList<>(list);
   List<String> tmpList2 =getAllSortList(tmpList,s+tmpList.remove(i)); 
   allList.addAll(tmpList2);
  }
  return allList;
 }
 public static void main(String[] args) {
  List<String> orgList =new ArrayList<>();
  orgList.add("A");
  orgList.add("B");
  orgList.add("C");
  List<String> desList = getAllSortList(orgList,"");
  for (String des : desList) {
   System.out.println(des);
  }
 }
}
</pre><pre code_snippet_id="1824640" snippet_file_name="blog_20160811_3_7026884" name="code" class="java">


运行结果:

ABC
ACB
BAC
BCA
CAB
CBA

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP