二维数组和稀疏数组的相互转换步骤
1.创建一个二维数组(代码第19行)
2.将二维数组转换为稀疏数组(代码第40行)
3.将稀疏数组保存到磁盘中(代码第88行和130行)
4.读取磁盘中的文件,转换为稀疏数组(代码第96行和169行)
5.将稀疏数组转换为二维数组(代码第100行)
package com.shaocoder.sparse;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SparseArray {
public static void main(String[] args) {
int chessArr1[][] = new int[11][11];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
chessArr1[4][5] = 2;
chessArr1[3][7] = 1;
chessArr1[5][4] = 1;
System.out.println("原始的二维数组:");
for(int[] row : chessArr1) {
for(int data : row) {
System.out.print(data + " ");
}
System.out.println();
}
int sum = 0;
for(int i = 0; i < chessArr1.length; i++) {
for(int j = 0; j < chessArr1[0].length; j++) {
if(chessArr1[i][j] != 0) {
sum++;
}
}
}
int sparseArr1[][] = new int[sum + 1][3];
sparseArr1[0][0] = chessArr1.length;
sparseArr1[0][1] = chessArr1[0].length;
sparseArr1[0][2] = sum;
int count = 0;
for(int i = 0; i < chessArr1.length; i++) {
for(int j = 0; j < chessArr1[0].length; j++) {
if(chessArr1[i][j] != 0) {
count++;
sparseArr1[count][0] = i;
sparseArr1[count][1] = j;
sparseArr1[count][2] = chessArr1[i][j];
}
}
}
System.out.println();
System.out.println("转化后的稀疏数组:");
for(int i = 0; i < sparseArr1.length; i++) {
System.out.printf("%d\t%d\t%d\t\n", sparseArr1[i][0], sparseArr1[i][1], sparseArr1[i][2]);
}
saveSparse(sparseArr1);
int[][] sparseArr2 = readSparse();
if(sparseArr2 != null) {
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("\n还原后的二维数组:");
for(int[] row : chessArr2) {
for(int data : row) {
System.out.print(data + " ");
}
System.out.println();
}
} else {
System.out.println("readSparse()方法返回的结果为null");
}
}
public static void saveSparse(int[][] sparseArr){
FileWriter writeFile = null;
try {
File file = new File("sparseArray.txt");
if(!file.exists()) {
file.createNewFile();
}
writeFile = new FileWriter(file);
for(int i = 0; i < sparseArr.length; i++) {
for(int j = 0; j < 2; j++) {
writeFile.write(sparseArr[i][j] + ",");
}
writeFile.write(sparseArr[i][2] + "");
writeFile.write("\n");
}
writeFile.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(writeFile != null)
writeFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static int[][] readSparse() {
FileReader reader = null;
BufferedReader readerBuf = null;
int[][] sparseArr = null;
try {
reader = new FileReader("sparseArray.txt");
readerBuf = new BufferedReader(reader);
List<String> strList = new ArrayList<>();
String lineStr;
while((lineStr = readerBuf.readLine()) != null) {
strList.add(lineStr);
}
int lineNum = strList.size();
sparseArr = new int[strList.size()][3];
int count = 0;
for(String str : strList) {
String[] strs = str.split("\\,");
sparseArr[count][0] = Integer.valueOf(strs[0]);
sparseArr[count][1] = Integer.valueOf(strs[1]);
sparseArr[count][2] = Integer.valueOf(strs[2]);
count++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(readerBuf != null)
readerBuf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sparseArr;
}
}
运行结果