原二维数组
稀疏数组
package ShangGuiGu.sparseArray;
import java.io.*;
public class SparseArrayTest {
/**
* 将稀疏数组存盘
* @param sparseArray
*/
public static void saveToFile(int[][] sparseArray){
FileWriter fileWriter=null;
try {
fileWriter= new FileWriter(new File("D:\\sparseArray.data"));
for (int[] array : sparseArray) {
fileWriter.write(array[0]+"\t"+array[1]+"\t"+array[2]);
fileWriter.write("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 从磁盘中读取稀疏数组
* @return
*/
public static int[][] readFromFile(){
int[][] sparseArray2=null;
boolean isNotRead=false;
BufferedReader bufferedReader=null;
try {
bufferedReader=new BufferedReader(new FileReader(new File("D:\\sparseArray.data")));
String lineStr=null;
int curCount=0;
while ((lineStr=bufferedReader.readLine())!=null){
String[] tempStr=lineStr.split("\t");
if(!isNotRead){
sparseArray2=new int[Integer.parseInt(tempStr[2])+1][3];
sparseArray2[curCount][0]=Integer.parseInt(tempStr[0]);
sparseArray2[curCount][1]=Integer.parseInt(tempStr[1]);
sparseArray2[curCount][2]=Integer.parseInt(tempStr[2]);
curCount++;
isNotRead=true;
}else {
sparseArray2[curCount][0]=Integer.parseInt(tempStr[0]);
sparseArray2[curCount][1]=Integer.parseInt(tempStr[1]);
sparseArray2[curCount][2]=Integer.parseInt(tempStr[2]);
curCount++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sparseArray2;
}
public static void main(String[] args) {
int row=8,column=8;
int[][] sourceArray=new int[row][column];
sourceArray[1][2]=5;
sourceArray[1][3]=4;
sourceArray[2][5]=1;
sourceArray[2][7]=4;
sourceArray[7][1]=9;
//遍历原二维数组
System.out.println("----------原二维数组如下-----------");
int count=0;
for (int[] array : sourceArray) {
for (int i = 0; i <array.length ; i++) {
System.out.printf("%d ",array[i]);
if (array[i]>0){
count++;
}
}
System.out.println();
}
int[][] sparseArray=new int[count+1][3];
sparseArray[0][0]=row;
sparseArray[0][1]=column;
sparseArray[0][2]=count;
//转为稀疏数组
System.out.println("------------稀疏数组如下------------");
int cur=0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if(sourceArray[i][j]>0){
cur++;
sparseArray[cur][0]=i;
sparseArray[cur][1]=j;
sparseArray[cur][2]=sourceArray[i][j];
}
}
}
//遍历稀疏数组
for (int[] array : sparseArray) {
System.out.printf("%d %d %d\n",array[0],array[1],array[2]);
}
/**
* 将稀疏数组存盘
*/
saveToFile(sparseArray);
/**
*从磁盘中读取稀疏数组
*/
int[][] sparseArray2=readFromFile();
//转化为二维数组
int row2=sparseArray2[0][0];
int column2=sparseArray2[0][1];
int[][] sourceArray2=new int[row2][column2];
for (int i = 1; i < sparseArray2.length; i++) {
sourceArray2[sparseArray2[i][0]][sparseArray2[i][1]]=sparseArray2[i][2];
}
//遍历二维数组
System.out.println("-----------原二维数组如下-----------");
for (int[] array : sourceArray2) {
for (int i = 0; i <array.length ; i++) {
System.out.printf("%d ",array[i]);
if (array[i]>0){
count++;
}
}
System.out.println();
}
}
}
运行结果如下