先来一段代码:
public class SparseArray {
public static void main(String[] args) {
//假设棋盘为 11 * 11 的二维数组
// 0表示没有棋子,1表示黑子,2表示白字
int[][] chessArr1 = new int[11][11];
// 将第二行,第三列设置为1
chessArr1[1][2] = 1;
// 将第三行,第四列设置为2
chessArr1[2][3] = 2;
// 遍历棋盘
System.out.println("=============原始棋盘=========");
for (int[] rows : chessArr1) {
for (int data : rows) {
System.out.printf("%dt", data);
}
System.out.println();
}
}
可以看到结果为:
=============原始棋盘========= 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
棋盘大部分值都为默认0,因此记录很多没有意义的值,因此可以使用稀疏数组来保存该数组。
原理如下图(借鉴韩老师的原理图):
稀疏数组实现,并将其写入文件:
//将二维数组转成稀疏数组
// sum 表示棋盘非0值的个数
int sum = 0;
for (int[] ints : chessArr1) {
for (int anInt : ints) {
if (anInt != 0) {
sum++;
}
}
}
//创建对应稀疏数组
int[][] sparseArr = new int[sum + 1][3];
//给稀疏数组赋值
// 表示棋盘行数
sparseArr[0][0] = 11;
// 表示棋盘列数
sparseArr[0][1] = 11;
// 表示棋盘非0的值
sparseArr[0][2] = sum;
//遍历二维数组,将非0值存入
int count = 0;
for (int i = 0, chessArr1Length = chessArr1.length; i < chessArr1Length; i++) {
for (int j = 0, intsLength = chessArr1[i].length; j < intsLength; j++) {
if (chessArr1[i][j] != 0) {
count++;
//行
sparseArr[count][0] = i;
//列
sparseArr[count][1] = j;
//值
sparseArr[count][2] = chessArr1[i][j];
}
}
}
//将数据存放进文件
String destFile = "d:\sparsArr.txt";
//这里使用字符流,异常可以直接抛出
BufferedWriter bw = new BufferedWriter(new FileWriter(destFile));
System.out.println();
System.out.println("===========稀疏数组为========");
//遍历稀疏数组,并写入文件
for (int i = 0; i < sparseArr.length; i++) {
System.out.printf("%dt%dt%dtn", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
// 以 " " 为分隔符
bw.write(sparseArr[i][0] + " " + sparseArr[i][1] + " " + sparseArr[i][2] + "n");
}
bw.close();
System.out.println();
得到的结果为:
===========稀疏数组为======== 11 11 2 1 2 1 2 3 2
可以看到,相比原始棋盘,稀疏数组更为美观,简便,如若需要还原二维数组,可以读取文件内容。代码如下:
//稀疏数组恢复成二维数组
//读取文件
BufferedReader br = new BufferedReader(new FileReader(destFile));
String line = null;
// 创建Sparse类集合,用于保存行列值
// Sparse为自定义类
ArrayList sparseList = new ArrayList<>();
while ((line = br.readLine()) != null){
// 以 " " 分隔
String[] rcv = line.split(" ");
// 创建实例对象,保存相关值
Sparse sparse = new Sparse(rcv[0], rcv[1], rcv[2]);
sparseList.add(sparse);
}
// 棋盘相关值
Sparse sparse0 = sparseList.get(0);
int rows = Integer.parseInt(sparse0.getRows());
int col = Integer.parseInt(sparse0.getCol());
int[][] chessArr2 = new int[rows][col];
//读取稀疏数组后几行数据,赋给原始棋盘
for (int i = 1; i < sparseList.size(); i++) {
chessArr2[sparseArr[i][0]][sparseArr[i][1]] = Integer.parseInt(sparseList.get(i).getVal());
}
//输出恢复后棋盘
System.out.println();
System.out.println("==========恢复后的棋盘=========");
for (int[] row : chessArr2) {
for (int date : row) {
System.out.printf("%dt", date);
}
System.out.println();
}
}
}
class Sparse{
//行
private String rows;
//列
private String col;
//值
private String val;
public Sparse(String rows, String col, String val) {
this.rows = rows;
this.col = col;
this.val = val;
}
public String getRows() {
return rows;
}
public void setRows(String rows) {
this.rows = rows;
}
public String getCol() {
return col;
}
public void setCol(String col) {
this.col = col;
}
public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
}
最后得到得到的结果如下:
==========恢复后的二维数组========= 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
可以看到和刚开始是一样的。
还在学习中,若有问题辛苦赐教。



