1、源码

  1. package com.study.sparsearray;
  2. import java.io.*;
  3. public class SparseArray2 {
  4. public static void main(String[] args) throws IOException {
  5. //创建一个11*11的二维数组
  6. int chessArr1[][] = new int[11][11];
  7. //给数组赋值
  8. chessArr1[1][2]=1;
  9. chessArr1[2][3]=2;
  10. chessArr1[3][4]=3;
  11. chessArr1[4][5]=4;
  12. //遍历数组
  13. for (int[] arr : chessArr1) {
  14. for (int data : arr) {
  15. System.out.print("\t"+data);
  16. }
  17. System.out.println();
  18. }
  19. //获取数组中不为0的数的个数
  20. int sum =0;
  21. for (int i = 0; i < 11; i++) {
  22. for (int j = 0; j < 11; j++) {
  23. if(chessArr1[i][j]!=0){
  24. sum++;
  25. }
  26. }
  27. }
  28. //将二维数组转换成稀疏数组 (sum+1)*3
  29. int sparseArr1[][] = new int[sum+1][3];
  30. sparseArr1[0][0]=11;
  31. sparseArr1[0][1]=11;
  32. sparseArr1[0][2]=sum;
  33. int count=1;
  34. for (int i = 0; i < 11; i++) {
  35. for (int j = 0; j < 11; j++) {
  36. if(chessArr1[i][j]!=0){
  37. sparseArr1[count][0]=i;
  38. sparseArr1[count][1]=j;
  39. sparseArr1[count][2]=chessArr1[i][j];
  40. count++;
  41. }
  42. }
  43. }
  44. //遍历稀疏数组
  45. System.out.println("sparseArr1为:");
  46. for (int[] arr : sparseArr1) {
  47. for (int data : arr) {
  48. System.out.print("\t"+data);
  49. }
  50. System.out.println();
  51. }
  52. //将稀疏数组存到文件中
  53. FileWriter fw = new FileWriter("SparseArray2");
  54. for (int i = 0; i < sparseArr1.length; i++) {
  55. for (int j = 0; j < 3; j++) {
  56. fw.write(sparseArr1[i][j]+"\t");
  57. }
  58. fw.write("\r\n");
  59. }
  60. fw.close();
  61. //从文件中将稀疏数组读取出来
  62. BufferedReader br = new BufferedReader(new FileReader("SparseArray2"));
  63. String line;
  64. int row=0;
  65. int sparseArr2[][] = new int[sum+1][3];
  66. while (((line=br.readLine())!=null)){
  67. String[] s = line.split("\t");
  68. for (int i = 0; i < s.length; i++) {
  69. sparseArr2[row][i]=Integer.parseInt(s[i]);
  70. }
  71. row++;
  72. }
  73. br.close();
  74. //将sparseArr2遍历
  75. System.out.println("sparseArr2为:");
  76. for (int[] arr : sparseArr2) {
  77. for (int data : arr) {
  78. System.out.print("\t"+data);
  79. }
  80. System.out.println();
  81. }
  82. //将从文件读取的稀疏数组转换成二维数组
  83. int chessArr2[][] = new int[sparseArr2[0][0]][sparseArr2[0][1]];
  84. for (int i = 1; i < sparseArr2.length; i++) {
  85. chessArr2[sparseArr2[i][0]][sparseArr2[i][1]] = sparseArr2[i][2];
  86. }
  87. for (int[] arr : chessArr2) {
  88. for (int data : arr) {
  89. System.out.print("\t"+data);
  90. }
  91. System.out.println();
  92. }
  93. }
  94. }

2、运行结果

图片.png

3、总结

  • 二维数组 转 稀疏数组的思路

    • 遍历 原始的二维数组,得到有效数据的个数 sum
    • 根据sum 就可以创建 稀疏数组 sparseArr int[sum + 1] [3]
    • 将二维数组的有效数据数据存入到 稀疏数组
  • 稀疏数组转原始的二维数组的思路

    • 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的 chessArr2 = int [11][11]
    • 在读取稀疏数组后几行的数据,并赋给 原始的二维数组 即可.

4、扩展

将稀疏数组存储到文件中,并读取出来

//将稀疏数组存到文件中
FileWriter fw = new FileWriter("SparseArray2");
for (int i = 0; i < sparseArr1.length; i++) {
    for (int j = 0; j < 3; j++) {
        fw.write(sparseArr1[i][j]+"\t");
    }
    fw.write("\r\n");
}
fw.close();

//从文件中将稀疏数组读取出来
BufferedReader br = new BufferedReader(new FileReader("SparseArray2"));
String line;
int row=0;
int sparseArr2[][] = new int[sum+1][3];
while (((line=br.readLine())!=null)){

    String[] s = line.split("\t");
    for (int i = 0; i < s.length; i++) {
        sparseArr2[row][i]=Integer.parseInt(s[i]);
    }
    row++;
}
br.close();