栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

稀疏数组代码解释

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

稀疏数组代码解释

 

参考链接:狂神说java

public class Demo3 {
    public static void main(String[] args) {
        //创建一个二维数组
        int[][] array = new int[11][11];
        array[1][2] = 1;
        array[2][3] = 1;
        System.out.println("输出原始数组");
        for (int[] ints : array) {
            for (int anInt : ints) {
                System.out.print(anInt + "t");
            }
            System.out.println();
        }
        System.out.println("+++++++++++++++++++++++++++++++++++++++");
        //转换为稀疏数组
        //获取有效值个数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (array[i][j] != 0) {
                    sum += 1;
                }
            }
        }
        System.out.println("有效值个数" + sum);
        //创建稀疏数组
        int[][] array2 = new int[sum + 1][3];
        array2[0][0] = 11;
        array2[0][1] = 11;
        array2[0][2] = sum;
        //遍历二维数组,将非零的值存放稀疏数组中
        int count = 0;
        //遍历行
        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];
                }
            }
        }
        //输出稀疏数组
        System.out.println("输出稀疏数组");
        for (int i = 0; i < array2.length; i++) {
            System.out.println(
                    //存放行的值
                    array2[i][0] + "t"
                            //存放列的值
                            + array2[i][1] + "t"
                            //存放具体的值
                            + array2[i][2] + "t");
        }
        System.out.println("还原数组");
        System.out.println("++++++++++++++++++++++++++++++++++");
        //读取稀疏数组中记录的行列值
        int[][] array3 = new int[array2[0][0]][array2[0][1]];
        //还原值,从稀疏数组的1行开始读,0行不用读
        for (int i = 1; i < array2.length; i++) {
            //还原在原始数组中的值
            array3[array2[i][0]][array2[i][1]] = array2[i][2];
        }
        System.out.println("输出还原数组");
        for (int[] ints : array) {
            for (int anInt : ints) {
                System.out.print(anInt + "t");
            }
            System.out.println();
        }
    }
}

输出结果:

输出原始数组
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    1    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    1    
还原数组
++++++++++++++++++++++++++++++++++
输出还原数组
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    1    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    
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/445255.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号