• 二维数组与稀疏数组的转换


    package com.summer.sparseArray;
    
    /**
     * @author :summer
     * @date :Created in 2022/8/23 09:12
     * @description:1、二维数组转稀疏数组 2、稀疏数组转二维数组
     */
    public class SparseArray {
        public static void main(String[] args) {
            //定义二维数组
            int[][] array = new int[11][11];
            array[0][2] = 6;
            array[1][3] = 2;
    
            //打印二维数组
            System.out.println("原始二维数组为:");
            for (int[] ints : array) {
                for (int anInt : ints) {
                    System.out.printf("%d\t", anInt);
                }
                System.out.println();
            }
            //计算有效的数值个数
            int sum = 0;
            for (int[] ints : array) {
                for (int anInt : ints) {
                    if (anInt != 0) {
                        sum++;
                    }
                }
            }
            //创建稀疏数组 并复制第一行
            int[][] sparseArray = new int[sum + 1][3];
            sparseArray[0][0] = 11;
            sparseArray[0][1] = 11;
            sparseArray[0][2] = sum;
            //为稀疏数组填充其余数据
            int count = 0;
            for (int i = 0; i < 11; i++) {
                for (int j = 0; j < 11; j++) {
                    if (array[i][j] != 0) {
                        count++;
                        sparseArray[count][0] = i;
                        sparseArray[count][1] = j;
                        sparseArray[count][2] = array[i][j];
                    }
                }
            }
    
            System.out.println("稀疏数组为:");
    
            for (int[] ints : sparseArray) {
                for (int anInt : ints) {
                    System.out.printf("%d\t", anInt);
                }
                System.out.println();
            }
    
            //稀疏数组再转为二维数组
            //为二维数组填充数据
            int[][] array1 = new int[sparseArray[0][0]][sparseArray[0][1]];
            for (int i = 1; i < sparseArray.length; i++) {
                array1[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
            }
            //打印二维数组
            System.out.println("转换的二维数组为:");
            for (int[] ints : array1) {
                for (int anInt : ints) {
                    System.out.printf("%d\t", anInt);
                }
                System.out.println();
            }
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
  • 相关阅读:
    应广单片机实现红蓝双色爆闪灯
    创意作品管理软件 Bridge 2024 mac中文版 br2024功能特色
    【JAVASE】程序异常处理
    如果线性变换可以模仿
    232.用栈实现队列(LeetCode)
    MySQL连接方式: Unix套接字 & TCP/IP
    Linux生产者消费者模型
    ChatGPT AIGC 快速合并Excel工作薄 Vlookup+INDIRECT
    MongoDB 遇见 spark(进行整合)
    企业级安全架构
  • 原文地址:https://blog.csdn.net/weixin_40612082/article/details/126487111