美文网首页
二维数组与稀疏数组互相转换

二维数组与稀疏数组互相转换

作者: 喵喵不吃鱼哦 | 来源:发表于2020-02-11 13:42 被阅读0次

    1.二维数组转换为稀疏数组

    2.稀疏数组转换二维数组

    public class 二维数组与稀疏数组 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int [][] arr = {
                    {0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,8,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,25,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,20},
                    {0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,5,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,1,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0}
                    };
            System.out.println("得到数组的长度为:"+getNum(arr, arr.length, arr[0].length));
            
            int [][] arrChange = new int[getNum(arr, arr.length, arr[0].length)+1][3];
            
            changeER(arr,11,11,arrChange,getNum(arr, arr.length, arr[0].length));
            
            for(int i = 0;i <= getNum(arr, arr.length, arr[0].length);i ++) {
                for(int j = 0;j < 3;j ++) {
                    
                    System.out.print(arrChange[i][j]+"    ");
                    
                }
                
                System.out.println();
                
            }   
            int returnArr[][] = changeXS(arrChange);//定义新的二维数组用于存放转换之后的二维数组
            
            for(int i = 0;i < returnArr.length;i ++) {
                for(int j = 0;j < returnArr[0].length;j ++) {
                    
                    System.out.print(returnArr[i][j]+"    ");
                    
                }
                
                System.out.println();
                
            }
            
        }
        private static int getNum(int [][] arr,int row,int col) {
            //得到共有多少有效数据
            int dataNum = 0;
            for(int i = 0;i < row; i ++) {
                for(int j = 0;j < col;j ++) {
                    if(arr[i][j] != 0) {
                        dataNum ++ ;
                    }
                }
            }
            return dataNum;
        }
        private static void changeER(int [][] arr,int row,int col,int [][]arrChange,int num) {//将二维数组转换为稀疏数组
            int n = 1;
            arrChange[0][0] = row;
            arrChange[0][1] = col;
            arrChange[0][2] = num;
            for(int i = 0;i < row; i ++) {
                for(int j = 0;j < col;j ++) {
                    if(arr[i][j] != 0) {
                        arrChange[n][2] = arr[i][j];
                        arrChange[n][0] = i;
                        arrChange[n++][1] = j;
                    }
                }
            }
        }
        private static int [][] changeXS(int [][]arrChange) {
            int [][] arr = new int [arrChange[0][0]][arrChange[0][1]];
            for(int i = 1;i < arrChange.length; i++) {
                arr[arrChange[i][0]][arrChange[i][1]] = arrChange[i][2];
            }
            return arr;
            
        }
    }
    

    相关文章

      网友评论

          本文标题:二维数组与稀疏数组互相转换

          本文链接:https://www.haomeiwen.com/subject/yycxfhtx.html