稀疏数组的使用(棋盘<===>稀疏数组)

package itheima_shuju01;

public class chessArry01_xishushuzu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int chessArry[][] = new int[11][11];
        chessArry[1][2] = 1;
        chessArry[2][3] = 2;
        int acount = 0;
        //棋盘输出
        System.out.println("棋盘输出");
        for(int []a1 :chessArry) {
            for(int shucu : a1) {
                System.out.print(shucu+" ");
                if(shucu != 0 ) {
                    acount++;
                }
            }
            System.out.println();
        }
        //棋盘--->稀疏数组
        int shuArry[][] = new int[acount+1][3];
        shuArry[0][0] = 11;
        shuArry[0][1] = 11;
        shuArry[0][2] = acount;
        int a=0;
        for(int i =0;i<11;i++) {
            for(int j=0;j<11;j++) {
                if(chessArry[i][j] != 0 ) {
                    a++;
                    shuArry[a][0] = i;
                    shuArry[a][1] = j;
                    shuArry[a][2] = chessArry[i][j];
                    
                }
            }
        }
        //稀疏数组输出
        System.out.println("稀疏数组输出");
        for(int []a1 :shuArry) {
            for(int shucu : a1) {
                System.out.print(shucu+" ");
            }
            System.out.println();
        }
        //稀疏数组--->棋盘
        System.out.println("稀疏数组--->棋盘");
        int chessArry1[][] = new int[shuArry[0][0]][shuArry[0][1]];
        
        for (int i1=1;i1<acount+1;i1++) {
        
            chessArry1[shuArry[i1][0]][shuArry[i1][1]]=shuArry[i1][2];
            
        }
        System.out.println("棋盘输出");
        for(int []a1 :chessArry1) {
            for(int shucu : a1) {
                System.out.print(shucu+" ");

            }
            System.out.println();
        }
//        for(int []a1 :chessArry) {
//            for(int shucu : a1) {
//                if(shucu != 0&&chessArry[a][2]==0) {
//                    chessArry1[a][2] = shucu;
//                    
//                    a++;
//                }else {
//                    a++;
//                }
//            }
//        }
    }

}

代码运行图:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值