# 多个数组排列问题

2019/06/19 17:10

[a,1,A],[a,1,B],[a,1,F],[a,2,A],[a,2,B],[a,2,F],[b,1,A],[b,1,B]......[d,2,A],[d,2,B],[d,2,F].

List<String[]> originalList


int[] tempIndexArr = new int[originalList.size()];

int[] tempLengthArr = new int[originalList.size()];


List<String[]> combineList = new ArrayList<>();


boolean completeFlag = false;


int changeIndex = originalList.size() - 1;

boolean isRightIndex = false;

while (!isRightIndex) {

tempIndexArr[changeIndex] += 1;

if(tempIndexArr[changeIndex] >= tempLengthArr[changeIndex]) {

tempIndexArr[changeIndex--] = 0;

} else {

isRightIndex = true;

}

}


String[] newItem = new String[originalList.size()];

for (int i = 0; i < originalList.size() ; i++) {

newItem[i] = originalList.get(i)[tempIndexArr[i]];

}



package work;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

/**
* @author lixiaoming
* @date 2019/6/19 16:53
*/
public class CombineTest {

public static void main(String[] args) {
List<String[]> originalList = new ArrayList<>();
originalList.add(new String[] {"a", "b", "c", "d"});
originalList.add(new String[] {"1", "2"});
originalList.add(new String[] {"A", "B", "F"});

List<String[]> combineList = getCombineList(originalList);

System.out.println("组成的新集合共有" + combineList.size() + "个数组");

combineList.forEach(s-> {
System.out.println(StringUtils.join(s, ","));
});
}

private static List<String[]> getCombineList(List<String[]> originalList) {
int originalSize = originalList.size();
int[] tempIndexArr = new int[originalSize];
tempIndexArr[originalSize - 1] = -1;
int[] tempLengthArr = new int[originalSize];
for (int i = 0; i < originalSize ; i++) {
tempLengthArr[i] = originalList.get(i).length;
}

List<String[]> combineList = new ArrayList<>();
boolean completeFlag = false;

while(!completeFlag) {
int changeIndex = originalList.size() - 1;
boolean isRightIndex = false;
while (!isRightIndex) {
tempIndexArr[changeIndex] += 1;
if(tempIndexArr[changeIndex] >= tempLengthArr[changeIndex]) {
if(changeIndex == 0) {
isRightIndex = true;
completeFlag = true;
} else {
tempIndexArr[changeIndex--] = 0;
}
} else {
isRightIndex = true;
}
}
if(isRightIndex && !completeFlag) {
String[] newItem = new String[originalList.size()];
for (int i = 0; i < originalList.size() ; i++) {
newItem[i] = originalList.get(i)[tempIndexArr[i]];
}
}
}

return combineList;
}

}



a,1,A

a,1,B

a,1,F

a,2,A

a,2,B

a,2,F

b,1,A

b,1,B

b,1,F

b,2,A

b,2,B

b,2,F

c,1,A

c,1,B

c,1,F

c,2,A

c,2,B

c,2,F

d,1,A

d,1,B

d,1,F

d,2,A

d,2,B

d,2,F

Process finished with exit code 0

0
0 收藏

0 评论
0 收藏
0