- 稀疏数组的实际应用
- 稀疏数组的基本介绍
- 处理方法
- 举例说明
- 思路分析
- 代码实现
1.稀疏数组的实际应用
-
将五子棋的存盘和续上盘
image-20220318134933354 -
分析问题:
- 因为该二维数组很多值都是默认值0,因此记录了很多没有意义的数据 -> 稀疏数组
2. 基本介绍
- 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组
2.1 处理方法
- 记录数组的 一共有几行几列,有多少个不同 的值
- 把具有不同值的元素的行列及值记录在一个小规模的数组中,从而 缩小程序 的规模
2.2 举例说明

image-20220318135449062
3. 思路分析

image-20220320210121938
-
二维数组 转 稀疏数组的思路
- 遍历 原始二维数组,得到有效数据的个数
sum - 根据
sum就可以创建稀疏数组int[][] sparseArr = int[sum+1][3] - 将二维数组的有效数据传入到稀疏数组
- 遍历 原始二维数组,得到有效数据的个数
-
稀疏数组 转 原始二维数组 的思路
- 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,如上图
chessArr = new int[11][11]; - 再读取稀疏数组中后几行的数据,
第一列的值为原始数组的行,而第二列为原始数组的列,第三列为之前行列原始数组元素的值,将值赋给原始二维数组
- 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,如上图
4. 代码实现
import java.io.*;
/**
* @author feng
* @create 2022-02-13 20:52
*/
public class SparseArray {
public static void main(String[] args) throws IOException {
// 创建一个原始的二维数组11*11
//0: 表示没有棋子,1表示黑子,2表示蓝子
int chessArr1[][] = new int[11][11];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
//输出原始的二维数组
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 < 11; i++){
for(int j = 0; j < 11; j++){
if(chessArr1[i][j] != 0){
sum++;
}
}
}
//2.创建对应的稀疏数组
int sparseArr[][] = new int[sum + 1][3];
//给稀疏数组赋值
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = sum;
// 遍历二维数组,将非0的存储到 sparseArr中
int count = 0; //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("得到的稀疏数据为~~~");
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();
//稀疏数组 ---》 二维数组
// 1.先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
//2.在读取稀疏数组里后几行的数据,并赋给原始二维数组
for (int i = 1; i < sparseArr.length; i++) {
chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
//输出恢复后的二维数组
System.out.println("恢复后的二维数组");
for(int[] row:chessArr2){
for(int data:row){
System.out.printf("%d\t",data);
}
System.out.println();
}
//将稀疏数组写入文件
writeToTxt(sparseArr,"D:\\Desktop\\map.data");
//从文件中读取稀疏数组
int[][] sparseArray2 = readFormTxt("D:\\Desktop\\map.data");
System.out.println();
System.out.println("从文件中读取到的稀疏数组");
for(int[] row:sparseArray2){
for(int data:row){
System.out.printf("%d\t",data);
}
System.out.println();
}
}
/**
* 从文件中读取稀疏数组
* @param
*/
private static int[][] readFormTxt(String path) {
File file = new File(path);
int[][] sparseArray = new int[0][]; // 稀疏数组
BufferedReader bufferedReader = null; // 缓冲流,提高效率,一行一行的读取
try {
bufferedReader = new BufferedReader(new FileReader(file));
// 获取稀疏数组的大小,方便初始化
int temp = 0;
while(bufferedReader.readLine() != null){
temp++;
}
sparseArray = new int[temp][3];
// 读完之后,关闭流,不然后面读到的为空
bufferedReader.close();
//重新打开流
bufferedReader = new BufferedReader(new FileReader(file));
String strs; // 读的每一行都是字符串,用strs存储
int i = 0; //指向稀疏数组的指针
int j = 0; //指向稀疏数组的指针
while((strs = bufferedReader.readLine() )!= null){
String[] str = strs.split(" "); // 将读取到的字符串根据 ”“分开,得到一个数组
for(String s : str){ //遍历该数组
sparseArray[i][j] = Integer.parseInt(s); // 将字符串转换成 int 存储在字符串上
j++;//指向列的指针每遍历以下自增一次
}
j = 0;// 一次遍历后,指向列的指针要重写设为0
i++;// 指针列自增
}
} catch (IOException e) {
e.printStackTrace();
}
if(bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sparseArray;
}
/**
* 将稀疏数组写入文件
* @param array
* @param path
*/
public static void writeToTxt(int[][] array,String path) throws IOException {
File file = new File(path);
//创建字符输出流
FileWriter writer = null;
try {
writer = new FileWriter(file,true);
//写数据
for(int i = 0;i < array.length;i++){
writer.write(array[i][0] + " ");
writer.write(array[i][1] + " ");
writer.write(array[i][2] + "" + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
if(writer != null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
