Java--算法--稀疏数组

  1. 稀疏数组的应用场景:
  2. 基本介绍:

    1.  

       

  3. 稀疏数组的应用实例:

  4.  

    package com.model.DataStructures;
    
    import java.io.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    
    /**
     * @Description:测试类
     * @Author: 张紫韩
     * @Crete 2021/6/23 11:39
     * 稀疏数组的实现 棋盘案例
     */
    public class SparseArrayDemo01 {
        public static void main(String[] args) throws IOException {
            /**
             * 创建一个原始的二维数组代替棋盘
             * 白棋代表1
             * 黑棋代表2
             *
             * */
            int[][] arr1 = new int[11][11];
            //二行三列
            arr1[1][2]=1;
            //三行四列
            arr1[2][3]=2;
            System.out.println("原始的数组");
            for (int[] arr2:arr1){
                for (int val:arr2){
                    System.out.printf("%d\t",val);
                }
                System.out.println();
            }
    
            System.out.println("*********************");
            System.out.println("转成稀疏数组");
            //找出原始数组中的元素个数
            int sum=0;
            for (int i = 0; i < arr1.length; i++) {
                for (int j = 0; j <arr1[i].length; j++) {
                    if (arr1[i][j]!=0) {
                        sum++;
                    }
                }
            }
            int[][] sparseArr = new int[sum+1][3];
            sparseArr[0][0]=11;
            sparseArr[0][1]=11;
            sparseArr[0][2]=sum;
            int row=1;
            for (int i = 0; i < arr1.length; i++) {
                for (int j = 0; j <arr1[i].length; j++) {
                    if (arr1[i][j] != 0) {
                      sparseArr[row][0]=i;
                      sparseArr[row][1]=j;
                      sparseArr[row][2]=arr1[i][j];
                      row++;
                    }
                }
            }
            for (int i = 0; i < sparseArr.length; i++) {
                for (int j = 0; j <sparseArr[i].length; j++) {
                        System.out.print(sparseArr[i][j]+"\t");
                }
                System.out.println();
            }
            System.out.println("*********************");
            System.out.println("将稀疏数组保存到文件中");
            String filePath="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_suanfa\\src\\main\\resources\\File\\map.data";
    
            BufferedWriter writer=new BufferedWriter(new FileWriter(filePath));
            for (int i = 0; i < sparseArr.length; i++) {
                for (int j = 0; j <sparseArr[i].length; j++) {
                   writer.write(sparseArr[i][j]+"\t");
                }
                writer.newLine();
            }
            writer.close();
            System.out.println("写入成功");
    
    
            System.out.println("*********************");
            System.out.println("读取文件中的稀疏数组,并转换未二维数组");
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            String readLine=null;
            String[] str=reader.readLine().split("\t");
            int len=Integer.parseInt(str[0]);
            int len1=Integer.parseInt(str[1]);
            int[][] sparseArr1=new int[len][len1];
            while ((readLine=reader.readLine())!=null){
                String[] f=readLine.split("\t");
                sparseArr1[Integer.parseInt(f[0])][Integer.parseInt(f[1])]=Integer.parseInt(f[2]);
            }
            reader.close();
            for (int i = 0; i < sparseArr1.length; i++) {
                for (int j = 0; j <sparseArr1[i].length; j++) {
                    System.out.print(sparseArr1[i][j]+"\t");
                }
                System.out.println();
            }
            System.out.println("成功读到文件中的稀疏数组");
    
    
    
            System.out.println("*********************");
    
            System.out.println("稀疏数组转换为普通的二维数组");
    
            int[][] arr2=new int[sparseArr[0][0]][sparseArr[0][1]];
            for (int i = 1; i < sparseArr.length; i++) {
                    arr2[sparseArr[i][0]][sparseArr[i][1]]=sparseArr[i][2];
    
            }
            for (int i = 0; i < arr2.length; i++) {
                for (int j = 0; j < arr2[i].length; j++) {
                    System.out.printf("%d\t",arr2[i][j]);
                }
                System.out.println();
            }
    
    
        }
    
    }

     

posted @ 2021-06-23 14:03  张紫韩  阅读(37)  评论(0编辑  收藏  举报