稀疏数组、txt文件存与读
@Description: 原始数组大小为11*11,0表示没有棋子,1表示黑子,2表示蓝子,
- 原始数组中只有一个黑子(第二行第三列)与一个蓝子(第三行第五列),
- 要求将该数组转稀疏数组,再将稀疏数组转回二维数组
- 拓展功能–>{原始数组中的有效值可以是任意数字,将稀疏数组保存到文件,从文件获取稀疏数组}
import java.io.*;
/**
* @Description: 原始数组大小为11*11,0表示没有棋子,1表示黑子,2表示蓝子,
* 原始数组中只有一个黑子(第二行第三列)与一个蓝子(第三行第五列),
* 要求将该数组转稀疏数组,再将稀疏数组转回二维数组
* 拓展功能-->{原始数组中的有效值可以是任意数字,将稀疏数组保存到文件,从文件获取稀疏数组}
* @author: Hobbit
* @date: 2022.02.20
*/
public class sparse {
public static void main(String[] args) throws IOException {
int beforeRow = 11;
int beforeCol = 11;// 增加代码可复用性,可以封装成函数
int[][] before = new int[beforeRow][beforeCol];
before[1][2] = 1;
before[0][8] = 1;
before[2][4] = 2;
before[3][5] = 2;
before[4][5] = 3;
before[7][10] = 7;
System.out.println("=============原始二维数组========================");
print(before);
int totalVal = 0;//有效值
for (int[] ints : before) {
for (int anInt : ints) {
if (anInt != 0) {
totalVal++;
}
}
}
int[][] sparse = new int[totalVal + 1][3];
sparse[0][0] = before.length;
sparse[0][1] = before[0].length;
sparse[0][2] = totalVal;
int sparseRow = 0;
for (int i = 0; i < before.length; i++) { //遍历行
for (int j = 0; j < before[0].length; j++) { //遍历列
if (before[i][j] != 0) {
sparseRow++;
sparse[sparseRow][0] = i;
sparse[sparseRow][1] = j;
sparse[sparseRow][2] = before[i][j];
}
}
}
System.out.println("=============二维数组转换后的稀疏数组========================");
print(sparse);
saveSparse(sparse);//保存稀疏数组
sparse = getSparse();//从文件读取稀疏数组
int[][] after = new int[sparse[0][0]][sparse[0][1]];
for (int i = 1; i < sparse.length; i++) {
after[sparse[i][0]][sparse[i][1]] = sparse[i][2];
}
System.out.println("=============稀疏数组转换后的二维数组========================");
print(after);
}
/**
* @Description: 读取稀疏数组
* @Param: []
* @return: int[][]
*/
private static int[][] getSparse() throws IOException {
File file = new File("src\\稀疏数组\\src\\sparse.txt");
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
BufferedReader br = new BufferedReader(isr);
br.mark((int) file.length() + 1); //将标记打在首行
int rows = 0;
int cols = 0;
while (br.readLine() != null) {
rows++;
}
br.reset();//重新从mark标记行开始读
String readLine = br.readLine();
br.reset(); //重新从mark标记行开始读
String[] split = readLine.split(" ");
cols = split.length;
int[][] sparse = new int[rows][cols];
int row = 0;
String line;
while ((line = br.readLine()) != null) {
String[] s = line.split(" ");
for (int i = 0; i < s.length; i++) {
sparse[row][i] = Integer.parseInt(s[i]);
}
row++;
}
isr.close();
br.close();
return sparse;
}
/**
* @Description: 保存稀疏数组
* @Param: [sparse]
* @return: void
*/
private static void saveSparse(int[][] sparse) throws IOException {
File file = new File("src\\稀疏数组\\src\\sparse.txt");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file));
for (int i = 0; i < sparse.length; i++) {
for (int j = 0; j < sparse[0].length; j++) {
osw.write(sparse[i][j] + "");
osw.write(" ");
}
osw.write("\n");
}
System.out.println("保存稀疏数组成功");
osw.close();
}
/**
* @Description: 打印二维数组
* @Param: [array]
* @return: void
*/
static void print(int[][] array) {
for (int[] ints : array) {
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
}
}