稀疏数组转数组面向对象实战操作
下面是优化过后的代码
/**
* @author 驳壳毛瑟
* @version 1.8
* @date 2022/01/02 20:16
*/
class Chess {
private int chessLen;
private int blackRow;
private int blackCol;
private int whiteRow;
private int whiteCol;
private int[][] wholeChess;
public Chess(int chessLen, int blackRow, int blackCol, int whiteRow, int whiteCol) {
this.chessLen = chessLen;
this.blackRow = blackRow;
this.blackCol = blackCol;
this.whiteRow = whiteRow;
this.whiteCol = whiteCol;
}
/**
* 获取添加棋子后的棋盘
*
* @return 添加棋子后的棋盘
*/
public int[][] getAllPieceChess() {
wholeChess = new int[chessLen][chessLen];
wholeChess[blackRow][blackCol] = 1;
wholeChess[whiteRow][whiteCol] = 2;
return wholeChess;
}
/**
* 打印期盼的方法
*/
public void printChessPlate() {
int[][] chessPlate = getAllPieceChess();
for (int i = 0; i < chessPlate.length; i++) {
for (int j = 0; j < chessPlate[i].length; j++) {
System.out.print(chessPlate[i][j] + " ");
}
System.out.println();
}
}
}
class ArrToSparseArr {
private int sum;
private int count;
private int chessLen;
private int[][] array;
private int[][] sparseArray;
public ArrToSparseArr(int[][] array, int chessLen) {
this.sum = 0;
this.count = 0;
this.array = array;
this.sparseArray = new int[][]{};
this.chessLen = chessLen;
}
/**
* 统计稀疏数组元素的数量
*
* @return 非0元素的数量
*/
public void countNumSparse() {
for (int i = 0; i < chessLen; i++)
for (int j = 0; j < chessLen; j++)
if (array[i][j] != 0) sum++;
}
/**
* 获取二维数组对应的稀疏数组
*/
public int[][] getWholeSparseArray() {
sparseArray = new int[sum + 1][3];
sparseArray[0][0] = chessLen;
sparseArray[0][1] = chessLen;
sparseArray[0][2] = sum;
for (int i = 0; i < chessLen; i++) {
for (int j = 0; j < chessLen; j++) {
if (array[i][j] != 0) {
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = array[i][j];
}
}
}
return sparseArray;
}
/**
* 打印稀疏数组
*/
public void printSparseArr() {
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]);
}
}
}
class SpareArrToArr {
private int[][] array;
private int[][] sparseArray;
public SpareArrToArr(int[][] sparseArray) {
this.sparseArray = sparseArray;
this.array = new int[sparseArray[0][0]][sparseArray[0][1]];
}
/**
* 读取稀疏数组的方法
*/
public void getWholeArray() {
for (int i = 1; i < sparseArray.length; i++) {
array[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
}
}
/**
* 打印二维数组的方法
*/
public void printArray() {
for (int[] ints : array) {
for (int anInt : ints) {
System.out.printf("%d ", anInt);
}
System.out.println();
}
}
}
class StoringReadingSparseArrayDisk {
private int[][] sparseArray;
private int[][] recoverySparseArray;
private List<Integer> list;
public StoringReadingSparseArrayDisk(int[][] sparseArray) {
this.sparseArray = sparseArray;
}
/**
* 稀疏数组存入磁盘
*
* @return 是否存储完毕
*/
public boolean storingSparseArray(String filepath) {
File file = new File(filepath);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
for (int[] row : sparseArray) {
for (int data : row) {
// 写入文件
bw.write(data + "\t");
}
// 换行
bw.write("\n");
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
/**
* 读取存储到磁盘的稀疏数组数据
*
* @return 是否读取完毕
*/
public boolean readingDisk(String filepath) {
File src = new File(filepath);
BufferedReader br = null;
list = new ArrayList<>();
try {
br = new BufferedReader(new FileReader(src));
String line;
while ((line = br.readLine()) != null) {
// 每循环1次读取1行数据
String[] str = line.split("\t");
for (int i = 0; i < str.length; i++) {
list.add(Integer.parseInt(str[i]));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
/**
* List集合转二维数组
*
* @return 转换后的二维数组
*/
public int[][] listToSparseArray() {
recoverySparseArray = new int[list.get(2) + 1][3];
int j = 0;
for (int i = 0; i < list.size(); i = i + 3) {
recoverySparseArray[j][0] = list.get(i);
recoverySparseArray[j][1] = list.get(i + 1);
recoverySparseArray[j][2] = list.get(i + 2);
j++;
}
return recoverySparseArray;
}
}
class ChessTest {
public static void main(String[] args) {
// 测试棋盘类
Chess chessPlate = new Chess(11, 1, 2, 2, 3);
int[][] allPieceChess = chessPlate.getAllPieceChess();
chessPlate.printChessPlate();
// 测试数组转稀疏数组类
ArrToSparseArr arrToSparseArr = new ArrToSparseArr(allPieceChess, allPieceChess.length);
arrToSparseArr.countNumSparse();
int[][] wholeSparseArray = arrToSparseArr.getWholeSparseArray();
arrToSparseArr.printSparseArr();
// 测试稀疏数组转数组类
SpareArrToArr spareArrToArr = new SpareArrToArr(wholeSparseArray);
spareArrToArr.getWholeArray();
spareArrToArr.printArray();
// 测试稀疏数组磁盘存取
StoringReadingSparseArrayDisk arrayDisk = new StoringReadingSparseArrayDisk(wholeSparseArray);
boolean isStorageSuccess = arrayDisk.storingSparseArray("D:\\wallhaven\\sparseArray.data");
System.out.println(isStorageSuccess);
boolean isReadingSuccess = arrayDisk.readingDisk("D:\\wallhaven\\sparseArray.data");
System.out.println(isReadingSuccess);
int[][] ints = arrayDisk.listToSparseArray();
SpareArrToArr spareArrToArr1 = new SpareArrToArr(ints);
spareArrToArr1.getWholeArray();
spareArrToArr1.printArray();
}
}