在编写五子棋程序中,有存盘退出和续上盘的功能。使用二位数组记录则会产生很多的无用数据。如图:
这样就会有很多的0占用空间,解决这个问题的方法是将其转换为稀疏数组。
例如:
第一行记录依次为:原数组的行数、列数、有多少非0值
第二行开始是记录每一个非0的数字在原数组中的位置。
那么题目中的稀疏数组就是:
转换为稀疏数组之后需要进行保存到文件中。整体思路如图:
二维数组转稀疏数组思路:
- 便利原始二维数组,得到有效数据个数sum
- 根据sum创建稀疏数组:sparseArr int[sum+1][3]
- 将二维数组的有效数据存入到稀疏数组
稀疏数组转原始二维数组的思路:
- 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组。
- 读取稀疏数组的数据,赋值给原始的二维数组
使用代码实现如下:
public class SparseArray {
public static void main(String[] args) {
int [][] chessArray = new int[11][11];
chessArray[1][2] = 1;
chessArray[2][3] = 2;
SparseArray sparseArray = new SparseArray();
int[][] sparseAssay = sparseArray.setSparseAssay(chessArray);
String path = "D:/map.data";
sparseArray.saveSparseArr(sparseAssay, path);
int[][] ints = sparseArray.readSparseArr(path);
for (int i = 0; i < ints.length; i++) {
System.out.printf("%d\t%d\t%d\n", ints[i][0], ints[i][1], ints[i][2]);
}
}
/**
* @Description: setSparseAssay 转换成为稀疏数组
* @param: [chessArray]
* @return: int[][]
* @auther: zqq
* @date: 20/5/16 20:48
*/
int [][] setSparseAssay(int [][] chessArray){
System.out.println("原始数组");
for (int[] row : chessArray) {
for (int item : row) {
System.out.printf("%d\t",item);
}
System.out.println();
}
int count = 0;
for (int i = 0; i < chessArray.length; i++) {
for (int j = 0; j < chessArray[i].length; j++) {
if (chessArray[i][j] != 0){
count++;
}
}
}
int [][] sparseArr = new int[count+1][3];
sparseArr[0][0] = chessArray.length;
sparseArr[0][1] = chessArray[0].length;
sparseArr[0][2] = count;
count = 0;
for (int i = 0; i < chessArray.length; i++) {
for (int j = 0; j < chessArray[i].length; j++) {
if (chessArray[i][j] != 0){
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArray[i][j];
}
}
}
System.out.println("稀疏数组");
for (int i = 0; i < sparseArr.length; i++) {
System.out.printf("%d\t%d\t%d\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
}
return sparseArr;
}
/**
* @Description: saveSparseArr 保存数组
* @param: [sparseArr, path]
* @return: void
* @auther: zqq
* @date: 20/5/16 20:28
*/
public void saveSparseArr(int [][] sparseArr, String path){
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter(path));
for (int i = 0; i < sparseArr.length; i++) {
for (int j = 0; j < sparseArr[i].length; j++) {
bufferedWriter.write(sparseArr[i][j]+"\t");
}
bufferedWriter.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bufferedWriter != null){
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @Description: readSparseArr 读取数组
* @param: [path]
* @return: int[][]
* @auther: zqq
* @date: 20/5/16 20:44
*/
public int[][] readSparseArr(String path){
BufferedReader bufferedReader = null;
int row = 0;
int[][] sparse = null;
try {
bufferedReader = new BufferedReader(new FileReader(path));
String line = null;
ArrayList<String> stringArrayList = new ArrayList<>();
while ((line = bufferedReader.readLine()) != null){
stringArrayList.add(line);
}
if (stringArrayList.size() != 0){
sparse = new int[stringArrayList.size()][3];
for (int i = 0; i < stringArrayList.size(); i++) {
String[] split = stringArrayList.get(i).split("\t");
for (int j = 0; j < split.length; j++) {
sparse[i][j] = Integer.valueOf(split[j]);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (sparse != null){
return sparse;
}
return null;
}
}