Java implements permutations and combinations between arrays
- 2021-01-19 22:15:50
- OfStack
The permutations and combinations of multiple arrays in Java are as follows
Description: There is a batch of phones in various colors, sizes, versions, and then to achieve a combination of various properties between them.
Define various attributes
String[] color={" red "," white "," blue "," golden "};
String[] size={"4.7 " ","5.1 " ","6.0 " "};
String[] version={" unicom "," telecom "," mobile "," All netcom "};
So let's look at the result 1
Red,4.7 inches, full Internet access,
Red,4.7 inches, move,
Red,4.7 inches, Telecom,
Red,4.7 inches, Unicom,
Red,5.1 inches, full Internet access,
White,5.1 inches, mobile,
White,5.1 inches, Unicom,
White,6.0 inches, full Internet access,... .posted so much, should be able to understand it
All right, let's do the code
public void doExchange(List arrayLists){
int len=arrayLists.size();
// Judge array size Is less than 2 If theta is less than theta, it means the recursion is done, otherwise you get the idea, don't you? Look at the code intermittently
if (len<2){
this.arrayLists=arrayLists;
return;
}
// Get the first 1 An 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 first 2 An array
String[] arr1= (String[]) arrayLists.get(1);
int len1=arr1.length;
// Calculates the current two arrays 1 How many combinations can be formed
int lenBoth=len0*len1;
// Defines a collection that temporarily holds collated data
ArrayList<ArrayList<String>> tempArrayLists=new ArrayList<>(lenBoth);
// The first 1 layer for Is the loop arrayLists The first 1 An element of
for (int i=0;i<len0;i++){
// The first 2 layer for Is the loop arrayLists The first 2 An element of
for (int j=0;j<len1;j++){
// Determine the first 1 If the element is an array, the loop is just beginning
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]);
// Adds permutation data to a temporary collection
tempArrayLists.add(arr);
}else {
// So this is the least explicit cycle 1 Come on, let's do it 1 The results of the round are taken out to continue with arrayLists Under the 1 Permutation of three elements
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 regenerated from the above permutation 1 A collection of
List newArrayLists=new ArrayList<>();
// Assemble the numbers that haven't been lined up yet, and see i=2 Because the first two arrays are done, there is no need to add them
for (int i=2;i<arrayLists.size();i++){
newArrayLists.add(arrayLists.get(i));
}
// Remember to add our painstakingly sorted data to the new set 1 B: Oh, otherwise it would be useless
newArrayLists.add(0,tempArrayLists);
// You read that right, the whole idea of this algorithm is recursion.
doExchange(newArrayLists);
}