利用稀疏矩阵压缩存储棋盘:二维数组-》稀疏矩阵-》文件
package hanshunping.sparsearray;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class StoreGame {
public static void main(String[] args) {
int[][] chessArray = new int[11][11] ;
chessArray[2][3] = 1;
chessArray[3][4] = 2;
chessArray[4][5] = 3;
StoreGame obj = new StoreGame();
int sum = obj.count(chessArray);
int[][] sparseArray= obj.toSparseArray(chessArray,sum);
obj.display(sparseArray);
obj.write2file(sparseArray);
}
public int[][] toSparseArray(int[][] chessArray,int sum){
System.out.println("变换中。。。。\n");
int[][] sparseArray = new int[sum+1][3];
sparseArray[0][1] = sparseArray[0][0] = chessArray.length;
sparseArray[0][2] = sum;
int count = 0;
for (int i=0;i<chessArray.length;i++){
for (int j=0;j<chessArray.length;j++){
if (chessArray[i][j] != 0){
count = count + 1;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chessArray[i][j];
}
}
}
return sparseArray;
}
public int count(int[][] chessArray){
int sum = 0;
for (int[] row : chessArray) {
for (int val : row) {
System.out.print(val+"\t");
if (val != 0){
sum = sum + 1;
}
}
System.out.println();
}
return sum;
}
public void display(int[][] sparseArray){
System.out.println("压缩后的稀疏矩阵:");
for (int i=0;i<sparseArray.length;i++) {
System.out.println(sparseArray[i][0]+"\t"+sparseArray[i][1]+"\t"+sparseArray[i][2]);
}
}
public void write2file(int[][] sparseArray){
File file = new File("data.txt");
FileWriter out = null;
try {
out = new FileWriter(file);
for(int i=0;i<sparseArray.length;i++){
for(int j=0;j<sparseArray[0].length;j++){
out.write(sparseArray[i][j]+"\t");
}
out.write("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
读档:文件->稀疏矩阵->二维数组
package hanshunping.sparsearray;
import java.io.*;
public class ReadGame {
public static void main(String[] args) {
ReadGame obj = new ReadGame();
File file = new File("data.txt");
int[][] spareArray = obj.readFile(file,3);
obj.toChessArry(spareArray);
}
public int[][] readFile(File file, int sum){
int[][] sparseArray = new int[sum+1][3];
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String str = bufferedReader.readLine();
int count2=0;
while (str != null && str !=""){
String[] rows = str.split("\t");
for (int i=0;i<rows.length;i++){
sparseArray[count2][i] = Integer.parseInt(rows[i]);
}
++count2;
str = bufferedReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sparseArray;
}
public void display(int[][] sparseArray){
System.out.println("重文件读取的稀疏矩阵:");
for (int i=0;i<sparseArray.length;i++) {
System.out.println(sparseArray[i][0]+"\t"+sparseArray[i][1]+"\t"+sparseArray[i][2]);
}
}
public int[][] toChessArry(int[][] sparseArray){
int[][] chessArray = new int[ sparseArray[0][0] ] [ sparseArray[0][1] ];
for (int i=1;i<sparseArray.length;i++) {
chessArray[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
}
System.out.println("恢复后的棋盘:\n");
for (int[] ints : chessArray) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println("\n");
}
return chessArray;
}
}