**
稀疏数组
**
##基本介绍:
当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。
稀疏数组的处理方法是:
1) 记录数组一共有几行几列,有多少个不同的值
2) 把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
如下图所示:
注意:这份代码是看了尚硅谷的韩顺平老师的课程进行的总结
package com.zlx.sparseArray;import java.io.*;
import java.util.ArrayList;
import java.util.List;public class SparseArray {public static void main(String[] args) {//创建一个原始的二维数组 11*11//0:表示没有棋子 1:表示黑子 2:表示蓝子int chessArr1[][] = new int[11][11]; //创建一个存放整数的11*11的二维数组chessArr1[1][2] = 1; //第2行第3列存一个 黑子chessArr1[2][3] = 2; //第3行第5列存一个 蓝子chessArr1[2][5] = 1; //第3行第5列存一个 蓝子//输出原始的二维数组//先循环遍历行for (int[] row:chessArr1) {//再循环遍历列for (int data:row) {System.out.printf("%d\t",data); //每行数据使用tab进行分割}System.out.println();//输出完一行进行换行}//将二维数组转为稀疏数组//1、先遍历二维数组,得到非0数据的个数int sum = 0;for (int i = 0; i < 11; i++) {for (int j = 0; j < 11; j++) {if(chessArr1[i][j] != 0){sum ++;}}}//2、创建对应的稀疏数组int sparseArr[][] = new int[sum+1][3]; //稀疏数组的行数:不为0的整数的个数+1 列数:3 即:row/col/val//给稀疏数组赋值sparseArr[0][0] = 11; //稀疏数组第一行第一列存放:原始数组的行数sparseArr[0][1] = 11; //稀疏数组的第一行第二列存放:原始数组的列数sparseArr[0][2] = sum; //稀疏数组的第一行第三列存放: 原始数组的非0的整数的个数//遍历原始的二维数组,将非0的值存放到稀疏数组sparseArray中//定义一个count用于记录当前是第几个非0数据int count = 0;for (int i = 0; i < 11; i++) {for (int j = 0; j < 11; j++) {if(chessArr1[i][j] != 0){count ++;sparseArr[count][0] = i;sparseArr[count][1] = j;sparseArr[count][2] = chessArr1[i][j];}}}System.out.println("--------------------转换后得到的稀疏数组为--------------------");System.out.println("方法1: ");//输出当前稀疏数组 方法1:for (int i = 0; i < sparseArr.length; i++) {for (int j = 0; j < 3; j++) {System.out.printf("%d\t",sparseArr[i][j]);}System.out.println();}//输出当前稀疏数组 方法2:System.out.println("方法2: ");for (int i = 0; i < sparseArr.length; i++) {System.out.printf("%d\t%d\t%d\t\n",sparseArr[i][0],sparseArr[i][1],sparseArr[i][2]);}System.out.println("+++++++++++++++++++++++++++将稀疏数组存入到硬盘中++++++++++++++++++++++++++");File dest = new File("E:\\sparesArray.data");//将稀疏数组存入到硬盘中try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(dest))){for (int[] row:sparseArr) {for (int data:row) {bufferedWriter.write(data + "\t"); //一行一行存}bufferedWriter.write("\n");//注意:BufferedWriter是缓冲输入流,意思是调用BufferedWriter的write方法时候。// 数据是先写入到缓冲区里,并没有直接写入到目的文件里。必须调用BufferedWriter的flush()方法。// 这个方法会刷新一下该缓冲流,也就是会把数据写入到目的文件里。// 或者你可以调用BufferedWriter的close()方法,该方法会在关闭该输入流之前先刷新一下该缓冲流。// 也会把数据写入到目的文件里。//如果没有在里面的for()循环中添加 bw.flush(); 这句话,// 在if 的时候重新 new BufferedWriter(); 就把原来bw(缓冲区)中的覆盖掉了。于是就不能写进文件字符。bufferedWriter.flush();}} catch (Exception e) {e.printStackTrace();}//从硬盘中读取稀疏数组并且进行恢复//获取硬盘中的稀疏数组的行数,并且将其存储到一个list集合中File src = new File("E:\\sparesArray.data");BufferedReader bufferedReader = null;List<Integer> list = new ArrayList<>();try {bufferedReader = new BufferedReader(new FileReader(src));String line;//java.io.BufferedReader.readline() 用于读取一行文本。遇到换行符或回车符readLine()方法会终止读取。//readLine()方法读出是null,就表示文件结尾了。while ((line = bufferedReader.readLine()) != null){String[] str = line.split("\t");for (int i = 0; i < str.length; i++) {list.add(Integer.parseInt(str[i]));}}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(bufferedReader != null){try {bufferedReader.close();} catch (IOException e) {e.printStackTrace();}}}System.out.println("list ====>>>> "+list);//打印稀疏数组行数System.out.println("稀疏数组的行数为: "+list.get(2)+1); //第三个元素代表不为0的元素有多少个,+1代表稀疏数组的行数//从硬盘读取稀疏数组到内存种的方法二:直接对之前的list进行操作//创建稀疏数组int sparseArr2[][]=new int[list.get(2)+1][3];int m=0;for(int i=0;i<list.size();i=i+3) {sparseArr2[m][0]=list.get(i);sparseArr2[m][1]=list.get(i+1);sparseArr2[m][2]=list.get(i+2);m++;}System.out.println("-----------------从硬盘种读取的稀疏数组---------------------------");for(int[] row2:sparseArr2) {for(int data:row2) {System.out.printf("%d\t",data);}System.out.println();}System.out.println("++++++++++++++++++=将稀疏数组转为普通的二维数组+++++++++++++++++++++++++++++");//将稀疏数组转为普通的二维数组//1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的 chessArr2 = int [11][11]//2. 在读取稀疏数组后几行的数据,并赋给 原始的二维数组 即可.int chessArr2[][] = new int[sparseArr2[0][0]][sparseArr2[0][1]];for (int i = 1; i < sparseArr2.length; i++) {//稀疏数组的第2行开始:第一列为原始数组的行 第二列为原始数组的列 第三列为原始数组的值chessArr2[sparseArr2[i][0]][sparseArr2[i][1]] = sparseArr2[i][2];}//方法1:for (int i = 0; i < chessArr2.length; i++) {for (int j = 0; j < chessArr2[i].length; j++) {System.out.printf("%d\t",chessArr2[i][j]);}System.out.println();}}
}
输出的结果为: