目标:代码实现将稀疏数组保存到磁盘,再从磁盘读取恢复原始数组。
代码如下
public class SparseArray03 {
public static void main(String[] args) throws FileNotFoundException {
//先创建11*11的原始数组
int[][] chessArray1 = new int[11][11];
///0:表示没有棋子 1:表示黑色棋子 2:表示蓝色棋子
chessArray1[1][2] = 1;
chessArray1[2][3] = 2;
//遍历二维数组拿到有效数据的个数,通过sum来存储
int sum = 0;
for(int i = 0; i < chessArray1.length;i++) {
for(int j = 0; j < chessArray1.length;j++) {
if(chessArray1[i][j] != 0) {
sum++;
}
}
}
//拿到原始数组中的数据后,我们就可以创建稀疏数组了
int[][] sparseArray = new int[sum+1][3];
//稀疏数组的第一行用来存储原始数组的大小和数据的个数
sparseArray[0][0] = 11;
sparseArray[0][1] = 11;
sparseArray[0][2] = sum;
//然后将二维数组的有效数据存入到除第一行外的稀疏数组中
int count = 1;//在此需要借助计数器将数据依次放入稀疏数组中,count从1开始记数
for(int i = 0; i < chessArray1.length;i++) {
for(int j = 0; j < chessArray1.length;j++) {
if(chessArray1[i][j] != 0) {
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chessArray1[i][j];
count++;
}
}
}
//稀疏数组创建成功后将其存入磁盘
File file = new File("D:/map.data");
if(file.exists()) {//判断文件是否存在
try {
file.createNewFile();//如果不存在则创建文件
} catch (IOException e) {
e.printStackTrace();
}
}
OutputStream os = new FileOutputStream(file);
for(int i = 0; i < sparseArray.length;i++) {
String msg = sparseArray[i][0]+"\t"+sparseArray[i][1]+"\t"+sparseArray[i][2]+"\n";
try {
os.write(msg.getBytes("utf-8"));
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
//从磁盘中读取数据,并还原原始数组
BufferedReader reader = new BufferedReader(new FileReader(file));
//准备需要恢复的二维数组
int[][] chessArray2 = null;
int count1 = 0;//计数器
String msg = "";
try {
while((msg = reader.readLine()) != null) {
String[] str = msg.split("\t");
Integer row = Integer.valueOf(str[0]);
Integer col = Integer.valueOf(str[1]);
Integer value = Integer.valueOf(str[2]);
if(count1 == 0) {
chessArray2 = new int[row][col];
count1++;
}else {
chessArray2[row][col] = value;
count1++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("恢复后的二维数组chessArray2~~");
for(int[] a : chessArray2) {
for(int b : a) {
System.out.print(b+" ");
}
System.out.println();
}
}
}