package sparsearray;
import java.io.*;
public class SparseArray {
public static void main(String[] args) {
int[][] chessArr1 = new int[11][11];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
System.out.println("原始二维数组:");
for (int[] row : chessArr1) {
for (int data : row) {
System.out.printf("%d\t",data);
}
System.out.println();
}
int count = 0;
for (int[] row : chessArr1) {
for (int data : row) {
if(data != 0) {
count++;
}
}
}
System.out.println("原始二维数组不为0的的个数为:"+count);
int[][] sparseArr = new int[count+1][3];
sparseArr[0][0] = chessArr1.length;
sparseArr[0][1] = chessArr1[0].length;
sparseArr[0][2] = count;
int count2 = 0;
for (int i = 1;i<chessArr1.length;i++) {
for (int j = 0;j<chessArr1[0].length;j++) {
if (chessArr1[i][j]!=0) {
count2++;
sparseArr[count2][0] = i;
sparseArr[count2][1] = j;
sparseArr[count2][2] = chessArr1[i][j];
}
}
}
System.out.println("");
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]);
}
System.out.println("开始写入文件<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
BufferedWriter out =null;
try {
out = new BufferedWriter(new FileWriter("E:\\java\\repos\\idea_repos\\DataStructures\\src\\sparsearray\\map.data",true));
for(int i = 0;i<sparseArr.length;i++) {
for (int j=0;j<sparseArr[0].length;j++) {
out.write(sparseArr[i][j]+"\t");
}
out.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out!=null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("写入文件成功<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
BufferedReader br = null;
int readRow = 0;
int[][] sparseArr2 = new int[count+1][3];
try {
br = new BufferedReader(new FileReader("E:\\java\\repos\\idea_repos\\DataStructures\\src\\sparsearray\\map.data"));
String line = null;
while ((line = br.readLine()) !=null) {
String[] temp = line.split("\t");
for (int j = 0;j<temp.length;j++) {
sparseArr2[readRow][j] = Integer.parseInt(temp[j]);
}
readRow++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("从磁盘读取的稀疏数组为:");
for (int i = 0;i<sparseArr2.length;i++) {
System.out.printf("%d\t%d\t%d\t\n",sparseArr2[i][0],sparseArr2[i][1],sparseArr2[i][2]);
}
int[][] chessArr2 = new int[sparseArr2[0][0]][sparseArr2[0][1]];
for(int i = 1; i < sparseArr2.length; i++) {
chessArr2[sparseArr2[i][0]][sparseArr2[i][1]] = sparseArr2[i][2];
}
System.out.println();
System.out.println("转换后的新的二维数组为:");
for (int[] row : chessArr2) {
for (int data : row) {
System.out.printf("%d\t",data);
}
System.out.println();
}
}
}