Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement permutation and combination algorithm in java

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to implement the permutation and combination algorithm in java. It is very detailed and has a certain reference value. Interested friends must read it!

Java permutation and combination algorithm [@ more@] import java.util.ArrayList;import java.util.List;/*** full permutation and combination algorithm * @ author mukeliang* 2009.10.21*/public class TestComposite {public static void main (String [] args) {/ / List allList = getFirstData (); / / List allList = getSecondData (); List allList = getThirdData (); List targetList = combinatePlan (allList); System.out.println (targetList) } / * for combinatorial arrangement * @ param sourceList* @ return List*/private static List combinatePlan (List sourceList) {int [] subSizeArray;int [] subCountArray;int sourceCount = sourceList.size (); / / length of large list subSizeArray = new int [sourceCount]; subCountArray = new int [sourceCount]; / / current value of sub-list int targetCount = 1 for (int I = 0; I)

< sourceCount; i++) {List subList = (List) sourceList.get(i);int size = subList.size();subSizeArray[i] = size; // 存放每个子list的长度targetCount *= size; // 最终要返回的list长度subCountArray[i] = 0; // 从0开始 每个都是0// a.println("subCountArray[i]:"+subCountArray[i]);}List targetList = new ArrayList(targetCount);// targetCountfor (int i = 0; i < targetCount; i++) {// 记录每个结果应该包含的元素个数List targetSub = new ArrayList(sourceCount);for (int k = 0; k < sourceCount; k++) {int subIndex = subCountArray[k];targetSub.add(((List) sourceList.get(k)).get(subIndex));}addCount(subSizeArray, subCountArray);targetList.add(targetSub);}return targetList;}/*** 计算List下标: 总是先数最后一位,最后一位数到最大值以后进一位* @param subSizeArray* @param subCountArray*/private static void addCount(int[] subSizeArray, int[] subCountArray) {// subCountArray数组的每个元素都是0 但是个数来记录循环要取的位置for (int i = subCountArray.length - 1; i >

= 0; if -) {if (subCountArray [I] < subSizeArray [I]-1) {subCountArray [I] +; return;} else {new ArrayList [I] = 0;} / * combination result: * list1 subscript list2 subscript * 0 0 * 0 1 * 0 2 * 1 0 * 1 2 * 2 1 * 2 * 2 * 2 * @ return List*/private static List getFirstData () {List list1 = new ArrayList (); list1.add ("Wang Yi") List1.add (Wang er); list1.add (Wang San); List list2 = new ArrayList (); list2.add (Zhang Yi); list2.add (Zhang er); list2.add (Zhang San); List allList = new ArrayList (); allList.add (list1); allList.add (list2); return allList } / * combination result: * list1 subscript list2 subscript list3 subscript * 00 * 0 01 * 0 10 * 0 11 * 1 00 * 1 01 * 1 10 * 11 * @ return List*/private static List getSecondData () {List list1 = new ArrayList (); list1.add ("Wang Yi"); list1.add ("Wang er"); List list2 = new ArrayList (); list2.add ("Zhang Yi"); list2.add ("Zhang er"); List list3 = new ArrayList () List3.add (Liu Yi); list3.add (Liu er); List allList = new ArrayList (); allList.add (list1); allList.add (list2); allList.add (list3); return allList;} / * combination result: * list1 subscript list2 subscript * 00 * 10 * @ return List*/private static List getThirdData () {List list1 = new ArrayList (); list1.add (Wang Yi); list1.add (Wang II); List list2 = new ArrayList () List2.add ("Zhang Yi"); List allList = new ArrayList (); allList.add (list1); allList.add (list2); return allList;}} these are all the contents of this article "how to implement permutation and combination algorithms in java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report