# 1.1.数组-稀疏数组 #### 1.定义 \*\*稀疏数组(Sparse Array)\*\*:当一个数组中的大部分元素为相同的值(0),可使用稀疏数组来保存该数组,可以将稀疏数组看做是普通数组的压缩,避免存储许多无用或相同数据而造成空间浪费。 #### 2.处理方法 1)记录数组一共有几行几列,和不同的值的个数。 2)把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模。 \`\`\`java 原始的二维数组(10\*10) 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \`\`\` \`\`\`java 稀疏数组 10 10 4 //原始二维数组的行,列,不同的值(非0值)的个数 1 2 1 //以下为每一个不同值的行,列,值 2 2 2 2 3 1 4 6 2 \`\`\` #### 3.实现思路 \*\*二维数组转稀疏数组的思路\*\* 1.遍历原始的二维数组,得到有效数据的个数sum。 2.根据sum就可以创建稀疏数组sparseArr int\[sum+1)\[3\] (稀疏数组始终是3列)。 3.将二维数组的有效数据数据存入到稀疏数组。 4.将稀疏数组存到本地磁盘中。 \*\*稀疏数组转原始的二维数组的思路\*\* 1.从本地磁盘中获取稀疏数组。 2.先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的chessArr2=int\[11\[11\]。 3.在读取稀疏数组后几行的数据,并赋给原始的二维数组即可。 #### 4.代码实例 \`\`\`java /\*\* \* 稀疏数组的代码实现--以实现五子棋程序为例 \* @author GreyPigeon mail:2371849349@qq.com \* @since 2023-12-12-10:49 \*\*/ public class SparseArray { public static void main(String\[\] args){ //创建一个原始的二维数组10\*10 //0:表示没有棋子,1表示黑子,2表示白子 int chessArr1\[\]\[\] = new int\[10\]\[10\]; chessArr1\[1\]\[2\] = 1; chessArr1\[2\]\[2\] = 2; chessArr1\[2\]\[3\] = 1; chessArr1\[4\]\[6\] = 2; //遍历原始二维数组 System.out.println("原始的二维数组"); for (int\[\] row: chessArr1){ for (int data: row){ System.out.printf("%d\\t", data); } System.out.println(); } //将二维数组转稀疏数组的思路 //1.先遍历二维数组得到非0数据的个数 int sum = 0; for(int i = 0; i \< chessArr1.length; i++){ for (int j = 0; j \< chessArr1.length; j++){ if (chessArr1\[i\]\[j\] != 0) { sum++; } } } //2.创建对应的稀疏数组 int sparseArr\[\]\[\] = new int\[sum + 1\]\[3\]; //稀疏数组的列固定为3 //给稀疏数组赋值 sparseArr\[0\]\[0\] = 10; //10行 sparseArr\[0\]\[1\] = 10; //10列 sparseArr\[0\]\[2\] = sum; //非0个数 //3.遍历二维数组,将非0数据存到sparseArr中 int count = 0; //count 用于记录是第几个非0数据 for(int i = 0; i \< chessArr1.length; i++){ for (int j = 0; j \< chessArr1.length; j++){ if (chessArr1\[i\]\[j\] != 0) { count++; sparseArr\[count\]\[0\] = i; sparseArr\[count\]\[1\] = j; sparseArr\[count\]\[2\] = chessArr1\[i\]\[j\]; } } } //输出稀疏数组 System.out.println("稀疏数组"); for (int\[\] row: sparseArr){ for (int data: row){ System.out.printf("%d\\t", data); } System.out.println(); } //4.将稀疏数存入磁盘文件 try { saveSparseArrayToDisk(sparseArr, "sparseArray.txt"); } catch (IOException e) { e.printStackTrace(); } System.out.println("--------------稀疏数组恢复成原始数组操作---------------"); //1.从文件获取稀疏数组 try { sparseArr = getSparseArrayFromDisk("sparseArray.txt"); } catch (IOException e) { e.printStackTrace(); } //输出稀疏数组 System.out.println("从文件获取稀疏数组"); for (int\[\] row: sparseArr){ for (int data: row){ System.out.printf("%d\\t", data); } System.out.println(); } //2.先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组。 int chessArr2\[\]\[\] = new int\[sparseArr\[0\]\[0\]\]\[sparseArr\[0\]\[1\]\]; //3.在读取稀硫数组后几行(第二行)的数据,并赋给原始的二维数组即可 for (int i= 1; i \< sparseArr.length; i++){ chessArr2\[sparseArr\[i\]\[0\]\]\[sparseArr\[i\]\[1\]\] = sparseArr\[i\]\[2\]; } //输出恢复后的二维数组 System.out.println(); System.out.println("恢复后的二维数组"); for (int\[\] row: chessArr2){ for (int data: row){ System.out.printf("%d\\t", data); } System.out.println(); } } /\*\* \* 将稀疏数组存入磁盘中 \* @param sparseArray \* @return void \*\*/ public static void saveSparseArrayToDisk(int\[\]\[\] sparseArray, String filePath) throws IOException { try { // 创建文件输出流 FileOutputStream fos = new FileOutputStream(filePath); // 创建 ObjectOutputStream 对象 ObjectOutputStream oos = new ObjectOutputStream(fos); // 将二维数组写入文件 oos.writeObject(sparseArray); // 关闭文件输出流 oos.close(); } catch (IOException e) { e.printStackTrace(); } } /\*\* \* 获取文件中存入的的稀疏数组 \* @param \* @return int\[\]\[\] \*\*/ public static int\[\]\[\] getSparseArrayFromDisk(String filePath) throws IOException { int\[\]\[\] sparseArray; try { // 创建文件输入流 FileInputStream fis = new FileInputStream(filePath); // 创建 ObjectInputStream 对象 ObjectInputStream ois = new ObjectInputStream(fis); // 读取二维数组 sparseArray = (int\[\]\[\]) ois.readObject(); // 关闭文件输入流 ois.close(); return sparseArray; } catch (IOException \| ClassNotFoundException e) { e.printStackTrace(); } return null; } } \`\`\` #### 5.应用场景 \* 数据序列化到磁盘上,减少数据量,在IO过程中提高效率。