稀疏数组

有时候,我们写一个游戏,比如说棋盘、俄罗斯方块、扫雷等,需要用二维数组来保存每个位置,记录每个位置不同的标记,因为有棋子或雷的地方肯定需要在数组中体现,比如说,下面这个数组,就代表着一个棋盘,为0的地方没有棋子,为1的地方存在黑色棋子,为2的地方存在白色棋子

0,0,0,0,0

0,1,0,0,0

0,0,0,2,0

0,0,0,0,0

这个棋盘目前有一枚黑色棋子和一枚白色棋子,其余没有棋子的地方都是0,这样的一个数组因为含有想大量的相同值,浪费了太多的空间,因此的,稀疏数组就应运而生了。

稀疏数组

当一个数组含有大量重复的相同的值的时候,这个数组就可以转化为稀疏数组进行表示

稀疏数组共有3列,第一行记录的是原数组的行数,列数,有效数据的个数,比如上面的就为2

接下来的每一行,三列分别记录的是第一个有效数据的行下标,列下标,有效数据的值,依次类推

上面的数组表示为稀疏数组为:

4,5,2

1,1,1

2,3,2

用代码实现数组转化为稀疏数组,存入文件,然后从文件中取出,最后再还原为原数组

原数组转化为稀疏数组的思路:

1、统计原数组的行数,列数,有效数据的个数

2、创建稀疏数组

3、有效数据填充到稀疏数组中

稀疏数组还原为原数组的思路:

1、获取稀疏数组第一行的信息,创建原数组

2、读取稀疏数组其他行的信息,填充创建的新数组

  1 /*
  2  * 原始数组转化为稀疏数组,
  3  * 稀疏数组保存到文件中
  4  * 从文件中读取稀疏数组
  5  * 还原为原数组
  6  */
  7 public class ArrayToSparsearray {
  8 
  9     public static void main(String[] args) {
 10         // 原始数组转化为稀疏数组
 11 
 12         int[][] arr = new int[10][10];
 13         arr[2][3] = 2;
 14         arr[3][4] = 3;
 15 
 16         // 原数组
 17         System.out.println("原数组为:");
 18         for (int i = 0; i < arr.length; i++) {
 19             for (int j = 0; j < arr[0].length; j++) {
 20                 System.out.print(arr[i][j] + "  ");
 21             }
 22             System.out.println();
 23         }
 24 
 25         // 求原数组有效数据的个数
 26         int sum = 0;// 用来存放有效数据的个数
 27         for (int[] is : arr) {
 28             for (int a : is) {
 29                 if (a != 0) {
 30                     sum++;
 31                 }
 32             }
 33         }
 34         System.out.println("有效数据个数sum=" + sum);
 35 
 36         // 创建稀疏数组
 37         int[][] sparseArray = new int[sum + 1][3];
 38         sparseArray[0][0] = arr.length;
 39         sparseArray[0][1] = arr[0].length;
 40         sparseArray[0][2] = sum;
 41 
 42         // 给稀疏数组填充数据
 43         int count = 1;
 44         for (int i = 0; i < arr.length; i++) {
 45             for (int j = 0; j < arr[0].length; j++) {
 46                 if (arr[i][j] != 0) {
 47                     sparseArray[count][0] = i;
 48                     sparseArray[count][1] = j;
 49                     sparseArray[count][2] = arr[i][j];
 50                     count++;
 51                 }
 52             }
 53         }
 54 
 55         System.out.println("稀疏数组为:");
 56         for (int i = 0; i < sparseArray.length; i++) {
 57             for (int j = 0; j < sparseArray[0].length; j++) {
 58                 System.out.print(sparseArray[i][j] + "	");
 59             }
 60             System.out.println();
 61         }
 62 
 63         // 稀疏数组存入文件中
 64         File file = new File("./a.txt");
 65         FileOutputStream fileOutputStream = null;
 66         try {
 67             file.createNewFile();
 68             fileOutputStream = new FileOutputStream(file);
 69             StringBuilder stringBuilder = new StringBuilder();
 70             for (int i = 0; i < sparseArray.length; i++) {
 71                 stringBuilder.append(sparseArray[i][0] + "  " + sparseArray[i][1] + "  " + sparseArray[i][2] + "
");
 72             }
 73             fileOutputStream.write(stringBuilder.toString().getBytes());
 74         } catch (IOException e) {
 75             e.printStackTrace();
 76         } finally {
 77             try {
 78                 fileOutputStream.close();
 79             } catch (IOException e) {
 80                 e.printStackTrace();
 81             }
 82         }
 83 
 84         // 从文件中读取稀疏数组
 85         FileReader fileReader = null;
 86         BufferedReader bufferedReader = null;
 87         int[][] sparseArray1 = new int[3][3];
 88         try {
 89             fileReader = new FileReader(file);
 90             bufferedReader = new BufferedReader(fileReader);
 91             String string = "";
 92             String[] arr1 = null;
 93             int t = 0;
 94             while ((string = bufferedReader.readLine()) != null) {
 95                 arr1 = string.split("  ");
 96                 sparseArray1[t][0] = Integer.valueOf(arr1[0]);
 97                 sparseArray1[t][1] = Integer.valueOf(arr1[1]);
 98                 sparseArray1[t][2] = Integer.valueOf(arr1[2]);
 99                 t++;
100             }
101         } catch (Exception e) {
102             e.printStackTrace();
103         } finally {
104             try {
105                 fileReader.close();
106                 bufferedReader.close();
107             } catch (IOException e) {
108                 e.printStackTrace();
109             }
110         }
111 
112         System.out.println("从文件读取的稀疏数组为:");
113         for (int i = 0; i < sparseArray1.length; i++) {
114             for (int j = 0; j < sparseArray1[0].length; j++) {
115                 System.out.print(sparseArray1[i][j] + "	");
116             }
117             System.out.println();
118         }
119 
120         // 读取的稀疏数组转化为原始数组sparseArray1——>arr2
121         int[][] arr2 = new int[sparseArray1[0][0]][sparseArray1[0][1]];
122         for (int i = 1; i < sparseArray1.length; i++) {
123             arr2[sparseArray1[i][0]][sparseArray1[i][1]] = sparseArray1[i][2];
124         }
125         
126         System.out.println("转化后的原数组为:");
127         for (int i = 0; i < arr2.length; i++) {
128             for (int j = 0; j < arr2[0].length; j++) {
129                 System.out.print(arr2[i][j] + "  ");
130             }
131             System.out.println();
132         }
133 
134     }
135 
136 }
原文地址:https://www.cnblogs.com/javatalk/p/11027361.html