日期:2014-05-20  浏览次数:20718 次

java 二维数组的问题 急! 第一次提问。。
二维数组,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里面,想了很久没有想出来...... 
望高手指教!!!!


------解决方案--------------------
Java code

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);
    }

}