20210328-算法学习-稀疏数组(sparsearray)-将稀疏数组保存到磁盘上,进行读取(超详细)
需求:将稀疏数组保存到磁盘上,读取磁盘的稀疏数组进行恢复(五子棋游戏)
1.(问题)拿五子棋游戏举例,如何通过稀疏数组来表示?
五子棋,棋盘该如何表示?
棋子(黑/白),该如何表示?
如果游戏中途不想玩了需要保存棋局,该怎么办?
如果想要恢复棋局,又该怎么办?
2.问题分析:
分析一下,我们可以用11*11的二维数组来表示棋盘,二维数组的值来代表棋子,二维数组值1表示黑子,数组值2表示白子
如果需要实现保存棋盘的功能,那么就把这二维数组保存起来
但是棋盘这二维数组有好多空值,直接保存起来会把空值也一并保存,直接保存感觉很浪费,那么有什么方法可以简化这个二维数组呢?
保存前:
图1.1
保存后:
图1.2
图1.2中,第一行是棋盘行/列及,棋盘中的有效值sum,第二行第一位是棋子所在的列,第二位是棋子的行,以此类推
图1.1中,可以看到该数组有11行11列,其中有两个不同的值,其位置在(1.2)其位置的值是1,(2.3)其位置的值是2,对比图1.1与图1.2我们把11*11的规模变成了3*3的规模,大大的做到了简化,这样不知不觉中就引入到了稀疏数组
3.稀疏数组小结:
当一个数组中大部分元素为0时,或者为同一个值得数组时,可以使用稀疏数组来保存该数组
3.1.稀疏数组的处理方式:
记录一共有几行几列,有多少个不同的值,把具有不同值得元素的行和列及数组的值记录在一个小规模的数组中,从而缩小,程序的规模
图1.3
4.代码如下:
package com.atAlgorithmTest;
/**
* @Author:
* @Date: 2021/3/28 15:53
*/
import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
/**
* @ClassName SparseArraySaveRead
* @Description将稀疏数组保存到磁盘上,进行读取
* @Author DELL
* @Date 2021/03/28 15:53
**/
public class SparseArraySaveRead {
public static void main(String[] args) {
//棋盘-获取一段11*11的数组
int[][] chessBoard = new int[11][11];
//向数组中存入两个值
chessBoard[0][1] = 1;//黑子
chessBoard[1][2] = 2;//白子
for (int[] row : chessBoard) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
System.out.println();
//创建一个稀疏数组
int sum = 0;//用来存储数组中的有效值
for (int row = 0; row < 11; row++) {
for (int colume = 0; colume < 11; colume++) {
if (chessBoard[row][colume] != 0) {//判断 去掉数组中的0值
sum++;
}
}
}
//稀疏数组
int count = 1;//用来记录数组中,第几个是非0数据
int[][] chessBoardSparse = new int[sum + 1][3];
chessBoardSparse[0][0] = 11;
chessBoardSparse[0][1] = 11;
chessBoardSparse[0][2] = sum;
for (int row = 0; row < 11; row++) {
for (int colume = 0; colume < 11; colume++) {
if (chessBoard[row][colume] != 0) {//去掉数组中的0值,保留有效值
chessBoardSparse[count][0] = row;//行
chessBoardSparse[count][1] = colume;//列
chessBoardSparse[count][2] = chessBoard[row][colume];//有效值
count++;
}
}
}
//打印稀疏数组
for (int[] row : chessBoardSparse) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
//将数组保存到本地的txt文件中
File saveSparseArray = new File("D:\\book\\SparseArraySaveRead.txt");
try {
FileWriter fileWriter = new FileWriter(saveSparseArray);
for (int i = 0; i < chessBoardSparse.length; i++) {
for (int j = 0; j < 3; j++) {
fileWriter.write(chessBoardSparse[i][j] + "\t");
}
fileWriter.write("\r\n");
}
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("存入文件时发生异常: " + e.getMessage().toString());
}
//读取保存的SparseArraySaveRead.txt文件
File readSparseArray = new File("D:\\book\\SparseArraySaveRead.txt");
int [][] textSparseArray = new int[3][3];
String line;//存放每行的数据
int row = 0;//记录行数
if(readSparseArray.exists()){
try {
BufferedReader bf = new BufferedReader(new FileReader(readSparseArray));
while ((line = bf.readLine()) !=null){
String[] temp = line.split("\t");
for (int j = 0; j<temp.length; j++){
textSparseArray[row][j] = Integer.parseInt(temp[j]);
}
row++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
// //读取保存的SparseArraySaveRead.txt文件-代码优化
// //如果不知道二维数组的行和列该如何处理?
// File readSparseArray = new File("D:\\book\\SparseArraySaveRead.txt");
// ArrayList<String> textSparseArray = new ArrayList<>();
//
// String line;
// if (readSparseArray.exists()) {
// try {
// BufferedReader bf = new BufferedReader(new FileReader(readSparseArray));
// while ((line = bf.readLine()) != null) {
// textSparseArray.add(line.trim());
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// if (textSparseArray != null && textSparseArray.size() > 0) {
// String[] firstLine = textSparseArray.get(0).split("\t");
// int rows = textSparseArray.size();//得到行数
// int columes = firstLine.length;//得到列数
// int[][] chessBoardSparse2 = new int[rows][columes];
//
// for (int i = 0; i < textSparseArray.size(); i++) {
// String[] oneLine = textSparseArray.get(i).split("\t");
// for (int j = 0; j < oneLine.length; j++) {
// chessBoardSparse2[i][j] = Integer.parseInt(oneLine[j]);
// }
// }
// System.out.println();
// System.out.println("从text中得到的稀疏数组: ");
//
// for (int[] lineData : chessBoardSparse2) {
// for (int data : lineData) {
// System.out.printf("%d\t", data);
// }
// System.out.println();
// }
// }
//验证
System.out.println();
System.out.println("从text中得到的稀疏数组: ");
for (int[] lineData : textSparseArray){
for (int data:lineData){
System.out.printf("%d\t",data);
}
System.out.println();
}
/**
* @Author lisongtao
* @Description : 将稀疏数组恢复成原始数组,具体思路如下:
* 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
* 在读取到稀疏数组后几行的数据,并赋给 原始的二维数组即可
* @Date 2021/3/28 19:54
* @Param [args]
* @return void
**/
int rowArr = textSparseArray[0][0];
int columeArr = textSparseArray[0][1];
int[][] recoverArr = new int[rowArr][columeArr];
for (int i = 1; i < textSparseArray.length; i++) {
int row1 = textSparseArray[i][0];
int colume1 = textSparseArray[i][1];
int value = textSparseArray[i][2];
recoverArr[row1][colume1] = value;
}
System.out.println();
System.out.println("复盘后的棋局: ");
for (int[] row1 : recoverArr) {
for (int data : row1) {
System.out.printf("%d\t", data);
}
System.out.println();
}
}
}

浙公网安备 33010602011771号