Java implements arrangement and combination of multiple arrays and java arrangement and combination
The arrangement and combination of multiple arrays in Java. The specific content is as follows:
Note: There are a batch of mobile phones with various colors, sizes, and versions, and then a combination of various attributes between them.
Define various attributes
String [] color = {"red", "White", "blue", "golden"}; String [] size = {"4.7", "5.1 ", "6.0"}; String [] version = {"Unicom", "Telecom", "mobile", "Netcom "};
Let's take a look at the results.
Red, 4.7, full Netcom,
Red, 4.7 inch, mobile,
Red, 4.7 inch, telecom,
Red, 4.7 inch, Unicom,
Red, 5.1, full Netcom,
White, 5.1 inch, mobile,
White, 5.1 inch, Unicom,
White, 6.0 inch, full Netcom ,.......... So much I can understand.
Okay, go to the code
Public void doExchange (List arrayLists) {int len = arrayLists. size (); // judge whether the array size is smaller than 2. If the value is smaller than 2, it indicates that recursion is completed. Otherwise, what do you know? View code intermittently if (len <2) {this. arrayLists = arrayLists; return;} // get the first array int len0; if (arrayLists. get (0) instanceof String []) {String [] arr0 = (String []) arrayLists. get (0); len0 = arr0.length;} else {len0 = (ArrayList <String>) arrayLists. get (0 )). size ();} // get the second array String [] arr1 = (String []) arrayLists. get (1); int len1 = arr1.length; // calculates the total number of combinations of the current two arrays. int lenBoth = len0 * len1; // define the ArrayList set for temporary storage of sorted data <ArrayLis T <String> tempArrayLists = new ArrayList <> (lenBoth); // the first layer of for is the for (int I = 0; I <len0; I ++) {// The second layer for is the for (int j = 0; j <len1; j ++) of the second element of the loop arrayLists) {// if the first element is an array, the loop is just starting. if (arrayLists. get (0) instanceof String []) {String [] arr0 = (String []) arrayLists. get (0); ArrayList <String> arr = new ArrayList <> (); arr. add (arr0 [I]); arr. add (arr1 [j]); // add the arranged data to the temporary set tempArrayLists. add (arr);} else {// Here we will see at least one round of loops, we take out the results of the previous round and continue to arrange ArrayList <String> arrtemp = (ArrayList <String>) arrayLists with the next element of arrayLists. get (0); ArrayList <String> arr = new ArrayList <> (); for (int k = 0; k <arrtemp. get (I ). size (); k ++) {arr. add (arrtemp. get (I ). get (k);} arr. add (arr1 [j]); tempArrayLists. add (arr) ;}}// this is a newly generated set List newArrayLists = new ArrayList <> () based on the preceding result (); // assemble the numbers that have not been arranged and check that I = 2, because the first two arrays have It's done. You don't need to add for (int I = 2; I <arrayLists. size (); I ++) {newArrayLists. add (arrayLists. get (I);} // remember to add the data we 've worked hard on to the first place in the new set. Otherwise, we're too busy with newArrayLists. add (0, tempArrayLists); // you are not mistaken. Our entire algorithm uses recursive thinking. DoExchange (newArrayLists );}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.