二维数组,length不固定,假设:
String[][] strArr = { { "00", "01" }, { "10", "11", "12" } };
现在要把strArr分解成
String[][] sep1={{"00"},{"10"}}
String[][] sep2={{"00"},{"10","11"}}
String[][] sep3={{"00"},{"10","11","12"}}
String[][] sep4={{"00", "01" },{"10"}}
String[][] sep5={{"00", "01" },{"10","11"}}
String[][] sep6={{"00", "01" },{"10","11","12"}}
然后放到collection里面,想了很久没有想出来......
望高手指教!!!!
String[][] strArr = { { "00", "01" }, { "10", "11", "12" } };
现在要把strArr分解成
String[][] sep1={{"00"},{"10"}}
String[][] sep2={{"00"},{"10","11"}}
String[][] sep3={{"00"},{"10","11","12"}}
String[][] sep4={{"00", "01" },{"10"}}
String[][] sep5={{"00", "01" },{"10","11"}}
String[][] sep6={{"00", "01" },{"10","11","12"}}
然后放到collection里面,想了很久没有想出来......
望高手指教!!!!
import java.util.ArrayList;public class ArrayConvert1 { ArrayList<String[][]> alist=new ArrayList<String[][]>();
public void conv(String[][] strArr){
int[] pos=new int[strArr.length];//保存需要复制的长度,0表示长度为1,依次类推。
while(pos[0]<strArr[0].length){
alist.add(createArray(strArr,pos));
addPos(strArr,pos);
}
} public void addPos(String[][] strArr,int[] pos){
int pow=1;
for (int i = pos.length-1; pow > 0 && i > 0; i--) {
pos[i]=(pos[i]+pow)%strArr[i].length;
pow=pos[i]==0?1:0;
}
pos[0]+=pow;
}
public String[][] createArray(String[][] strArr,int[] pos){
String[][] newarr=new String[strArr.length][];
for(int j=0;j<strArr.length;j++){
String[] arr=new String[pos[j]+1];
System.arraycopy(strArr[j], 0, arr, 0, pos[j]+1);
newarr[j]=arr;
//输出结果数组
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.print(" | ");
}
System.out.println();
return newarr;
}
public static void main(String[] args) {
String[][] strArr = { { "00", "01" }, { "10", "11", "12" },{ "20", "21", "22" } };
new ArrayConvert1().conv(strArr);
}}
这下终于明白多维数组是怎么回事了。
public class Main { public Main() {
start();
} public static void main(String[] args) {
new Main();
} private void start() {
int k = 1;
for (int i = 0; i < strArr.length; i++) {
k *= strArr[i].length;
} sep = new String[k][strArr.length][]; g(0); for (String[][] i : sep) {
for (String[] j : i) {
for (String t : j) {
System.out.print(t + " ");
}
System.out.print(" || ");
}
System.out.println("");
}
} private void g(int a) {
if (a >= strArr.length) {
sep[s] = (String[][]) temp.clone();
s++;
return;
} for (int i = 0; i < strArr[a].length; i++) {
temp[a] = new String[i + 1];
for (int j = 0; j < temp[a].length; j++) {
temp[a][j] = strArr[a][j];
}
g(a + 1);
}
}
private final String[][] strArr = {{"00", "01"}, {"10", "11", "12"}, {"20", "21"}};
private String[][][] sep;
private String[][] temp = new String[strArr.length][];
private int s = 0;
}
不过你的答案貌似很简单实用,多谢辛苦付出了,只可惜身上分不过,不然多给点了