This article describes the principles and implementation methods of Java Cartesian Integration Algorithm. Share it for your reference, as follows:
Java implementation of Cartesian product algorithm:
(1) In the loop, only one column moves down one cell at a time, which is the column pointed to by CounterIndex.
(2) If the column reaches the tail, the index of this column is reset to 0, and CounterIndex points to the previous column, which is equivalent to carrying, adding one of the index of the previous column.
(3) Finally, the number of rows generated is controlled to exit the loop.
public class Test { private static String[] aa = { "aa1", "aa2" }; private static String[] bb = { "bb1", "bb2", "bb3" }; private static String[] cc = { "cc1", "cc2", "cc3", "cc4" }; private static String[][] xyz = { aa, bb, cc }; private static int counterIndex = xyz.length - 1; private static int[] counter = { 0, 0, 0 }; public static void main(String[] args) throws Exception { for (int i = 0; i < aa.length * bb.length * cc.length; i++) { System.out.print(aa[counter[0]]); System.out.print("/t"); System.out.print(bb[counter[1]]); System.out.print("/t"); System.out.print(cc[counter[2]]); System.out.println(); handle(); } } public static void handle() { counter[counterIndex]++; if (counter[counterIndex] >= xyz[counterIndex].length) { counter[counterIndex] = 0; counterIndex--; if (counterIndex >= 0) { handle(); } counterIndex = xyz.length - 1; } }}Output 2*3*4=24 lines in total:
aa1 bb1 cc1aa1 bb1 cc2aa1 bb1 cc3aa1 bb1 cc4aa1 bb2 cc1aa1 bb2 cc2aa1 bb2 cc3aa1 bb2 cc4aa1 bb3 cc1aa1 bb3 cc2aa1 bb3 cc3aa1 bb3 cc4aa2 bb1 cc1aa2 bb1 cc2aa2 bb1 cc3aa2 bb1 cc4aa2 bb2 cc1aa2 bb2 cc2aa2 bb2 cc3aa2 bb2 cc4aa2 bb3 cc1aa2 bb3 cc3aa2 bb3 cc4
Recently I encountered a Cartesian product algorithm requirement. For example, the passed parameter is "1,3,6,7==4,5,8,9==3,4==43,45,8,9==35,4", and the returned list is, such as [1,4,3,43,35][1,4,3,43,4][1,4,3,45,35]..., the list contains 4*4*2*4*2=256 elements. The current idea is as follows:
import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class DescartesTest { /** * Get the Cartesian product of N sets* * Description: If the passed string is: "1,2,3==5,6==7,8" * Convert to a string array as: [[1, 2, 3], [5, 6], [7, 8]] * a=[1, 2, 3] * b=[5, 6] * c=[7, 8] * The sizes are: a_length=3, b_length=2, c_length=2, * The total size of the target list is: totalSize=3*2*2 = 12 * For each subset a, b, c, the number of loops = total number of records/(number of elements *number of Cartesian products of the subsequent set) * For each element in a, the number of loops = total number of records/(number of elements *number of Cartesian products of the subsequent set) = 12/(3*4) = 1 time, the number of times each element is printed per loop: the number of Cartesian products of the subsequent set = 2*2* For each element in b, the number of loops = total number of records/(number of elements *number of Cartesian products of the subsequent set) = 12/(2*2) = 3 times, the number of times each loop is printed per element: the number of Cartesian products of the subsequent set = 2* Number of times looping for each element in c = total number of records / (number of elements * number of Cartesian products of the subsequent set) = 12/(2*1) = 6 times, the number of times each element is printed per loop: the number of Cartesian products of the subsequent set = 1 * * Running result: * [[1, 2, 3], [5, 6], [7, 8]] 1,5,7, 1,5,8, 1,6,7, 1,6,8, 2,5,7, 2,5,8, 2,6,7, 2,6,8, 3,5,7, 3,5,8, 3,6,7, 3,6,8] From the result, we can see: each element in a loop per element, each element in b loop per element 3 times, each element in c loop per element 6 times, each time print 1 * * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String str ="1,3,6,7==4,5,8,9==3,4==43,45,8,9==35,4"; List<String> result = descartes(str); System.out.println(result); } @SuppressWarnings("rawtypes") public static List<String> descartes(String str) { String[] list = str.split("=="); List<List> strs = new ArrayList<List>(); for(int i=0;i<list.length;i++){ strs.add(Arrays.asList(list[i].split(","))); } System.out.println(strs); int total = 1; for(int i=0;i<strs.size();i++){ total*=strs.get(i).size(); } String[] mysesult = new String[total]; int now = 1; //The number of prints per element int itemLoopNum = 1; //The total number of loops per element int loopPerItem = 1; for(int i=0;i<strs.size();i++){ List temp = strs.get(i); now = now*temp.size(); //The index value of the target array is int index=0; int currentSize = temp.size(); itemLoopNum = total/now; loopPerItem = total/(itemLoopNum*currentSize); int myindex = 0; for(int j=0;j<temp.size();j++){ //The total number of loops per element is for(int k=0;k<loopPerItem;k++){ if(myindex==temp.size()) myindex=0; //The number of prints for each element per loop for(int m=0;m<itemLoopNum;m++){ mysesult[index]=(mysesult[index]==null?"":mysesult[index]+",")+((String)temp.get(myindex)); index++; } myindex++; } } } return Arrays.asList(mysesult); }}Run result output:
[[1, 3, 6, 7], [4, 5, 8, 9], [3, 4], [43, 45, 8, 9], [35, 4]]
[1,4,3,43,35, 1,4,3,43,4, 1,4,3,45,35, 1,4,3,45,4, 1,4,3,8,4, 1,4,3,9,35, 1,4,3,9,4, 1,4,4,43,35, 1,4,4,4,4,43,4, 1,4,4,4,4,4,4, 1,4,4,8,35, 1,4,4,4,9,35, 1,4,4,9,4, 1,5,3,43,4, 1,5,3,43,4, 1,5,3,45, 1,5,3,43,4, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,45, 1,5,3,8,35, 1,5,3,8,4, 1,5,3,9,35, 1,5,3,9,4, 1,5,4,43,35, 1,5,4,43,4, 1,5,4,45,35, 1,5,4,45,4, 1,5,4,8,35, 1,5,4,8,4, 1,5,4,9,35, 1,5,4,9,4, 1,8,3,43,35, 1,8,3,45, 1,8,3,45, 1,8,3,45, 1,8,3,45, 1,8,3,45, 1,8,3,8,4, 1,8,3,8,4, 1,8,3,8,4, 1,8,3,8,4, 1,8,3,9,35, 1,8,3,9,35, 1,8,3,9,35, 1,8,3,9,35, 1,8,3,9,35, 1,8,3,9,35, 1,8,3,9,35, 1,8,3,9,35, 1,8,3,9,35, 1,8,3,9,3,4, 1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8,35, 7,4,4,8,4,7,4,4,9,35, 7,4,4,9,35, 7,4,4,9,35, 7,4,4,9,4,7,5,3,43,4, 7,5,3,43,4, 7,5,3,45, 7,5,3,45, 7,5,3,45, 7,5,3,8,4, 7,5,3,9,35, 7,5,4,43,4, 7,5,4,43,4, 7,5,4,43,4, 7,5,4,43,4, 7,5,4,45,35, 7,5,4,45,4, 7,5,4,8,35, 7,5,4,8,4,7,5,4,8,4,7,5,4,9,35, 7,5,4,9,35, 7,5,4,9,4,7,8,3,43,4, 7,8,3,43,4, 7,8,3,45, 7,8,3,45, 7,8,3,45, 7,8,3,45, 7,8,3,8,4, 7,8,4,9,35, 7,8,4,9,35, 7,8,4,9,35, 7,8,4,43,4, 7,8,4,45, 7,8,4,45, 7,8,4,8,4,9,35, 7,8,4,9,35, 7,8,4,9,35, 7,8,4,4, 7,8,4,9,35, 7,8,4,9,35, 7,8,4,4, 7,8,4,9,35, 7,8,4,9,4,4, 7,8,4,9,35, 7,8,4,4, 7,8,4,9,35, 7,8,4,9,4,4, 7,8,4,9,35, 7,8,4,9,4,4, 7,8,4,9,35, 7,8,4,9,4,4, 7,9,3,43,35, 7,9,3,43,4, 7,9,3,45,35, 7,9,3,45,4, 7,9,3,8,4, 7,9,3,8,4, 7,9,3,9,4, 7,9,3,9,4, 7,9,4,43,35, 7,9,4,45, 7,9,4,45, 7,9,4, 7,9,4,8,35, 7,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9,4]
Recursive algorithm:
public static void fn(List<String[]> list,String[] arr,String str){//Iterate list List<String> li = new ArrayList<String>(); for(int i=0;i<list.size();i++){ //Get the current array if(i==list.indexOf(arr)){ //Iterate the array System.out.println(arr.length); for(String st : arr){ st = str + st; if(i<list.size()-1){ fn(list,list.get(i+1),st); }else if(i==list.size()-1){ li.add(st); } } } } for(int i = 0 ; i < li.size();i++ ) { System.out.println(li.get(i)); }}For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.