首页 > 编程语言 > 详细

稀疏数组练习

时间:2020-08-21 01:03:25      阅读:97      评论:0      收藏:0      [点我收藏+]
package array;

public class 稀疏数组7 {
    public static void main(String[] args) {
        //1、定义二维数组的棋盘11行11列,0是空棋,1白棋,2黑棋
        int[][] array = new int[11][11];

        //棋盘放入黑白各一个棋子
        array[1][2] = 1;
        array[2][3] = 2;

        //遍历棋盘,打印棋盘
        System.out.println("打印棋盘: ");
        for (int[] ints : array) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }
        System.out.println("-----------------------------------------");

        //2、计算棋盘上不为零的棋子
        int sum=0;
        for (int[] ints : array) {
            for (int anInt : ints) {
                if (anInt != 0){
                    sum++;
                }
            }
        }
        System.out.println("有效的棋子个数为: "+sum);
        System.out.println("-----------------------------------------");

        //3、打印稀疏数组
        /*例如
        11 11 2
        1  2  1
        2  3  2
        */

        int count=0;
        int[][] array2 = new int[sum+1][3];
        array2[0][0] = 11;
        array2[0][1] = 11;
        array2[0][2] = sum;

        //遍历array数组的值,存到array2内
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (array[i][j] != 0){
                    count++;
                    array2[count][0] = i;
                    array2[count][1] = j;
                    array2[count][2] = array[i][j];
                }
            }
        }

        //遍历取出array的值
        System.out.println("打印稀疏数组:");
        for (int[] ints : array2) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }
        System.out.println("-----------------------------------------");

        //4、根据array2的的表头11和11取得行和列数
        int[][] array3 = new int[array2[0][0]][array2[0][1]];

        //读取array2的稀疏数组,还原棋盘,因为稀疏数组的第一行是表示11行11列,有两个有效的数字,所以i=1,不取第0行
        for (int i = 1; i < array2.length; i++) {
            //读取array2的第二行的第一列和第二列横竖坐标,等于第三列的值
            array3[array2[i][0]][array2[i][1]] = array2[i][2];
        }

        //因array已经取得行和列和有效数字,因为遍历array3打印还原棋盘和棋子位置
        System.out.println("根据array2稀疏数组,还原棋盘和棋子: ");
        for (int[] ints : array3) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");       //print没有循环完第一行,不换行
            }
            System.out.println();   //因为第一行循环完毕换行打印
        }
    }
}
结果:
C:\Users\Administrator\.jdks\openjdk-14.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\lib\idea_rt.jar=7942:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Administrator\IdeaProjects\javaSe\out\production\基础语法 array.稀疏数组7 打印棋盘: 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 2 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 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 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ----------------------------------------- 有效的棋子个数为: 2 ----------------------------------------- 打印稀疏数组: 11 11 2 1 2 1 2 3 2 ----------------------------------------- 根据array2稀疏数组,还原棋盘和棋子: 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 2 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 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 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 进程已结束,退出代码 0

 

稀疏数组练习

原文:https://www.cnblogs.com/corn501/p/13538791.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!