这是改之前的代码
下面是优化过后的代码
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; } 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; } 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("%dt%dt%dtn", 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 Listlist; public StoringReadingSparseArrayDisk(int[][] sparseArray) { this.sparseArray = sparseArray; } 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; } 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; } 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(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)