The arrangement and combination of multiple Java arrays, the specific content is as follows
Note: There are a batch of mobile phones with various colors, sizes, and versions, and then they need to combine various attributes between them.
Define various attributes
String[] color={"red","white","blue","gold"};String[] size={"4.7 inches","5.1 inches","6.0 inches"};String[] version={"Unicom","Telecom","Mobile","Enterprise"};Let's see the results first
Red, 4.7 inches, full network access,
red, 4.7 inches, mobile,
Red, 4.7 inches, telecom,
Red, 4.7 inches, Unicom,
Red, 5.1 inch, full network access,
White, 5.1 inches, mobile,
White, 5.1 inch, Unicom,
White, 6.0 inches, full network access, ……………… I just posted so many, it should be understandable
OK, add the code
public void doExchange(List arrayLists){ int len=arrayLists.size(); //Judge whether the array size is less than 2. If it is less than, it means that the recursive completion has been completed. Otherwise, what do you understand, don’t understand? Look at the 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; //Calculate how many combinations of the two arrays can form int lenBoth=len0*len1; //Define the set of temporary storage of array data ArrayList<ArrayList<String>> tempArrayLists=new ArrayList<>(lenBoth); //The first layer for is the for the first element of the loop arrayLists (int i=0;i<len0;i++){ //The second layer for is the for the second element of the loop arrayLists (int j=0;j<len1;j++){ //Judge the first element if the first element is an array description, the loop has just begun 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 arrangement data to the temporary set tempArrayLists.add(arr); }else { //At this point, it is clear that there is at least one round. We take out the result of the previous round and continue to arrange the next element of arrayLists ArrayList<ArrayList<String>> arrtemp= (ArrayList<ArrayList<String>>) 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 collection regenerated based on the above arrangement List newArrayLists=new ArrayList<>(); //Pack up the array that has not been arranged and see clearly i=2, because the first two arrays are finished, there is no need to add them again for (int i=2;i<arrayLists.size();i++){ newArrayLists.add(arrayLists.get(i)); } //Remember to add the data we have worked hard to arrange to the first place in the new set, otherwise it will be useless to be newArrayLists.add(0,tempArrayLists); //You read that right, our entire algorithm uses the idea of recursion. doExchange(newArrayLists); }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.