该方法模拟的是将棋盘的位置保存到稀疏数组中,降低存储的数据量,通过写入磁盘做持久化,再读入后恢复棋盘内容。
package com.moson.sparsearray;
import java.io.*;
public class SparseArray {
public static void main(String[] args) {
int[][] cheerArray = new int[11][11];
cheerArray[1][2] = 1;
cheerArray[2][3] = 2;
cheerArray[3][6] = 1;
cheerArray[4][4] = 2;
System.out.println("原始数组~");
for (int[] row : cheerArray) {
for (int data : row) {
System.out.printf("%d\t" , data);
}
System.out.println();
}
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (cheerArray[i][j] != 0) {
sum++;
}
}
}
int[][] sparseArray = new int[sum+1][3];
sparseArray[0][0] = 11;
sparseArray[0][1] = 11;
sparseArray[0][2] = sum;
int count = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (cheerArray[i][j] != 0) {
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = cheerArray[i][j];
}
}
}
System.out.println("得到稀疏数组~");
for (int i = 0; i < sparseArray.length; i++) {
System.out.printf("%d\t%d\t%d\t\n", sparseArray[i][0], sparseArray[i][1], sparseArray[i][2]);
}
String pathname = "/Users/moxingjian/IdeaProjects/DataStructures/src/main/java/com/moson/sparsearray/saprseArray.txt";
File file = new File(pathname);
FileOutputStream fileOutputStream = null;
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
fileOutputStream = new FileOutputStream(file, true);
for (int i = 0; i < sparseArray.length; i++) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append( sparseArray[i][0] + "\t" + sparseArray[i][1] + "\t" + sparseArray[i][2] + "\n");
System.out.println(stringBuffer);
fileOutputStream.write(stringBuffer.toString().getBytes("utf-8"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("从文件中读取稀疏数组~");
File sparseArrayData = new File(pathname);
int[][] sourceCheerArray = null;
if (file.exists()) {
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
fileReader = new FileReader(sparseArrayData);
bufferedReader = new BufferedReader(fileReader);
String line = null;
int countSparse = 0;
int number = 0;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
String[] split = line.split("\t");
Integer row = Integer.valueOf(split[0]);
Integer col = Integer.valueOf(split[1]);
Integer value = Integer.valueOf(split[2]);
if (countSparse == 0) {
sourceCheerArray = new int[row][col];
} else {
number++;
sourceCheerArray[row][col] = value;
}
countSparse++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("得到原始二维数组~");
for (int[] row : sourceCheerArray) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
}
}