package com.atAlgorithmTest;
/**
* @Author: lisongtao
* @Date: 2021/3/28 15:53
*/
import java.io.*;
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;//白子
chessBoard[2][3] = 2;//白子
chessBoard[3][4] = 1;//黑子
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");
ArrayList<String> textSparseArray = new ArrayList<>();
String line;
if (readSparseArray.exists()) {
try {
BufferedReader bf = new BufferedReader(new FileReader(readSparseArray));
while ((line = bf.readLine()) != null) {
String[] text = line.split("\t");
for (int j = 0;j<text.length;j++){
textSparseArray.add(text[j]);
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
for (int i = 0; i < textSparseArray.size(); i++) {
System.out.println(textSparseArray.get(i));
}
/**
* @Author lisongtao
* @Description : 将稀疏数组恢复成原始数组,具体思路如下:
* 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
* 在读取到稀疏数组后几行的数据,并赋给 原始的二维数组即可
* @Date 2021/3/28 19:54
* @Param [args]
* @return void
**/
int [][] arrarr = new int[Integer.parseInt(textSparseArray.get(0))][Integer.parseInt(textSparseArray.get(1))];
for (int i = 1; i < textSparseArray.size()/3; i++) {
arrarr[Integer.parseInt(textSparseArray.get(i*3))][Integer.parseInt(textSparseArray.get(i*3+1))] = Integer.parseInt(textSparseArray.get(i*3+2));
}
for (int[] row1 : arrarr) {
for (int data : row1) {
System.out.printf("%d\t", data);
}
System.out.println();
}
}
}