原文地址:https://blog.csdn.net/guorui_java/article/details/106050189

一、稀疏数组

1.1 概念

当一个数组中部分元素是0,或者为同一个值的数组,可以使用稀疏数组来保存该数据。
稀疏数组的处理方法:
记录数组一共有几行几列,有多少个不同的值;
把具有不同值的元素的行列和值记录在一个小规模的数组中,从而缩小程序的规模。

1.2 存储

  1. 原数组中存在大量的无效数据,占据了大量的存储空间,真正有用的数据却少之又少;<br /> 压缩存储可以节省存储空间以避免资源的不必要的浪费,在数据序列化到磁盘时,压缩存储可以提高IO效率。<br />![](https://cdn.nlark.com/yuque/0/2021/png/1311515/1626231558902-44c899fb-a389-4b98-9c3e-80932a79834e.png#align=left&display=inline&height=649&margin=%5Bobject%20Object%5D&originHeight=649&originWidth=1434&size=0&status=done&style=none&width=1434)

二、代码实例

  1. package com.atguigu.sparsearray;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. public class SparseArray {
  6. public static void sparseArrayToIO(int[][] sparseArr) throws Exception{
  7. System.out.println("将稀疏数组存入磁盘");
  8. File file = new File("D:/sparseArr.txt");
  9. if(!file.exists()){
  10. file.createNewFile();
  11. }
  12. FileWriter writer = new FileWriter(file);
  13. for(int i =0; i < sparseArr.length; i++) {
  14. for(int j = 0; j < 3; j++) {
  15. writer.write(sparseArr[i][j]);
  16. }
  17. }
  18. writer.flush();
  19. writer.close();
  20. }
  21. //从磁盘中读取稀疏数组
  22. public static int[][] sparseArrFromIO(int lines) throws Exception {
  23. System.out.println("从磁盘中读取稀疏数组");
  24. FileReader reader = new FileReader("D:/sparseArr.txt");
  25. int getNum = 0;
  26. int[][] sparseArray = new int[lines][3];
  27. for(int i = 0;i < lines;i++) {
  28. for (int j = 0; j < 3; j++) {
  29. getNum = reader.read();
  30. sparseArray[i][j] = getNum;
  31. }
  32. }
  33. return sparseArray;
  34. }
  35. public static void ArrSparseArrTransfer() {
  36. //先创建一个原始的二维数组
  37. //0表示没有棋子,1表示黑子,2表示蓝子
  38. int chessArr1[][] = new int[11][11];
  39. chessArr1[1][2] = 1;
  40. chessArr1[2][3] = 2;
  41. //输出原始的二维数组
  42. System.out.println("原始的二维数组");
  43. for(int[] row : chessArr1) {
  44. for(int data : row) {
  45. System.out.printf("%d\t",data);
  46. }
  47. System.out.println();
  48. }
  49. //将二维数组转稀疏数组的思路
  50. //1、先遍历二维数组,得到非0数据的个数
  51. int sum = 0;
  52. for(int i = 0;i<11;i++) {
  53. for(int j = 0;j<11;j++) {
  54. if(chessArr1[i][j]!=0) {
  55. sum++;
  56. }
  57. }
  58. }
  59. //2、创建稀疏数组
  60. int sparseArr[][] = new int[sum+1][3];
  61. //3、稀疏数组赋值
  62. sparseArr[0][0] = 11;
  63. sparseArr[0][1] = 11;
  64. sparseArr[0][2] = sum;
  65. //遍历二维数组,将非0的值存放到稀疏数组中
  66. int count = 0;//count 用来记录第几个非0数据
  67. for(int i = 0;i<11;i++) {
  68. for(int j = 0;j<11;j++) {
  69. if(chessArr1[i][j]!=0) {
  70. count++;
  71. sparseArr[count][0] = i;
  72. sparseArr[count][1] = j;
  73. sparseArr[count][2] = chessArr1[i][j];
  74. }
  75. }
  76. }
  77. //输出稀疏数组的形式
  78. System.out.println("稀疏数组");
  79. for(int[] row : sparseArr) {
  80. for(int data : row) {
  81. System.out.printf("%d\t",data);
  82. }
  83. System.out.println();
  84. }
  85. int sparseArr2[][] = new int[sum+1][3];
  86. try {
  87. sparseArrayToIO(sparseArr);
  88. sparseArr2 = sparseArrFromIO(3);
  89. for(int[] row : sparseArr2) {
  90. for(int data : row) {
  91. System.out.printf("%d\t",data);
  92. }
  93. System.out.println();
  94. }
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. //稀疏数组恢复成原始的二维数组
  99. //1、先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
  100. int chessArr2[][] = new int[sparseArr2[0][0]][sparseArr2[0][1]];
  101. //2、再读取稀疏数组后几行的数据(从第二行开始),并赋给原始的二维数组即可
  102. for(int i = 1;i<sparseArr2.length;i++) {
  103. chessArr2[sparseArr2[i][0]][sparseArr2[i][1]] = sparseArr2[i][2];
  104. }
  105. //恢复后的二维数组
  106. System.out.println("恢复后的二维数组");
  107. for(int[] row : chessArr2) {
  108. for(int data : row) {
  109. System.out.printf("%d\t",data);
  110. }
  111. System.out.println();
  112. }
  113. }
  114. public static void main(String[] args) {
  115. ArrSparseArrTransfer();
  116. }
  117. }

三、控制台输出

1.稀疏数组 - 图1