将一个稀疏数组保存到磁盘上,命名为map.data。然后,再从磁盘读取map.data中的稀疏数组,将其恢复成原始的二维数组
一、代码如下:
import java.io.*;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
public class SparseArray {
public static void main(String[] args) {
int[][] chessArr1 = new int[10][10];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
chessArr1[3][4] = 3;
System.out.println("原始二维数组为:");
for(int[] a : chessArr1) {
for(int b : a) {
System.out.printf("%d\t", b);
}
System.out.println();
}
System.out.println("-----------------------------------------------");
int sum = 0;
for(int i = 0; i < chessArr1.length; i++) {
for(int j = 0; j < chessArr1[i].length; j++) {
if(chessArr1[i][j] != 0) {
sum++;
}
}
}
int[][] sparseArr = new int[sum + 1][3];
sparseArr[0][0] = chessArr1.length;
sparseArr[0][1] = chessArr1[0].length;
sparseArr[0][2] = sum;
int count = 0;
for(int i = 0; i < chessArr1.length; i++) {
for(int j = 0; j < chessArr1[i].length; j++) {
if(chessArr1[i][j] != 0) {
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr1[i][j];
}
}
}
System.out.println("稀疏数组为:");
for(int i = 0; i < sparseArr.length; i++) {
System.out.printf("%d\t%d\t%d\t\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
}
int[][] chessArr2 = new int[sparseArr[0][0]][sparseArr[0][1]];
for(int i = 1; i < sparseArr.length; i++) {
chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
System.out.println("-----------------------------------------------");
System.out.println("恢复的原始二维数组为:");
for(int[] a : chessArr2) {
for(int b : a) {
System.out.printf("%d\t", b);
}
System.out.println();
}
System.out.println("-----------------------------------------------");
System.out.println("-----------------------------------------------");
try {
System.out.println("将稀疏数组保存到磁盘并命名为map.data");
File f = new File("D:\\map.data");
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
System.out.println("写入中----------");
for(int i = 0; i < sparseArr.length; i++) {
osw.write(sparseArr[i][0] + "," + sparseArr[i][1] + "," + sparseArr[i][2] + ",");
}
osw.close();
fos.close();
System.out.println("写入磁盘成功");
System.out.println("读取中----------");
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
StringBuffer sb = new StringBuffer();
while(isr.ready()) {
sb.append((char)isr.read());
}
isr.close();
fis.close();
System.out.println("读取成功");
String ss = sb.toString();
String[] sb1 = sb.toString().split(",");
System.out.printf("从磁盘读取的字符串为:\n%s\n", ss);
int sum1 = 0;
int[][] sparseArr1 = new int[sb1.length/3][3];
sparseArr1[0][0] = Integer.parseInt(sb1[0]);
sparseArr1[0][1] = Integer.parseInt(sb1[1]);
sparseArr1[0][2] = Integer.parseInt(sb1[2]);
for(int i = 3; i < sb1.length; i += 3) {
sum1++;
sparseArr1[sum1][0] = Integer.parseInt(sb1[i]);
sparseArr1[sum1][1] = Integer.parseInt(sb1[i+1]);
sparseArr1[sum1][2] = Integer.parseInt(sb1[i+2]);
}
System.out.println("还原后的稀疏数组为:");
for(int i = 0; i < sparseArr1.length; i++) {
System.out.printf("%d\t%d\t%d\n", sparseArr1[i][0], sparseArr1[i][1], sparseArr1[i][2]);
}
int[][] chessArr3 = new int[sparseArr1[0][0]][sparseArr1[0][1]];
for(int i = 1; i < sparseArr1.length; i++) {
chessArr3[sparseArr1[i][0]][sparseArr1[i][1]] = sparseArr1[i][2];
}
System.out.println("还原后的二维数组为:");
for(int[] a : chessArr3) {
for(int b : a) {
System.out.printf("%d\t", b);
}
System.out.println();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
二、结果如下:
原始二维数组为:
0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0
0 0 0 0 3 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
-----------------------------------------------
稀疏数组为:
10 10 3
1 2 1
2 3 2
3 4 3
-----------------------------------------------
恢复的原始二维数组为:
0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0
0 0 0 0 3 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
-----------------------------------------------
-----------------------------------------------
将稀疏数组保存到磁盘并命名为map.data
写入中----------
写入磁盘成功
读取中----------
读取成功
从磁盘读取的字符串为:
10,10,3,1,2,1,2,3,2,3,4,3,
还原后的稀疏数组为:
10 10 3
1 2 1
2 3 2
3 4 3
还原后的二维数组为:
0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0
0 0 0 0 3 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