文章目录
- 将二维数组转化为稀疏数组
- 将稀疏数组存入文件
- 从文件中读出稀疏数组
- 将稀疏数组转化为二维数组
- 稀疏数组源码
将二维数组转化为稀疏数组
- 稀疏数组 :当一个二维数组中的有效数据远远小于无效数据的时候,我们可以对数据进行压缩,避免空间的浪费,稀疏数组有三列,第一列表示有效数据所在的行,第二行表示有效数据所在的列,第三行表示有效数据的值。在稀疏数组的第 0 行表示的是二维数组有多少行和多少列,第三列表示有多小有效的数据。
首先我们遍历二维数组,记录其中有多少个有效数据,然后就可以创建一个稀疏数组,第 0 行我们保存二维数组中有多少行、多少行和多少有效的数据。剩下的我们就保存有效数据所在的行和列,这样就可以把一个二维数组中的有效值记录在一个小规模的数组中,从而缩小程序的规模。
int chessArr[][] = new int[11][11];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
chessArr[3][4] = 3;
// for (int[] ints : chessArr) {
// for (int anInt : ints) {
// System.out.print(anInt + " ");
// }
// System.out.println();
// }
int m = chessArr.length, n = chessArr[0].length, cnt = 0;
for (int i = 0; i < m; ++ i) {
for (int j = 0; j < n; ++ j) {
if (chessArr[i][j] != 0) ++ cnt;
}
}
// System.out.println(cnt);
int sparseArray[][] = new int[cnt + 1][3];
sparseArray[0][0] = m;
sparseArray[0][1] = n;
sparseArray[0][2] = cnt;
int count = 0;
for (int i = 0; i < m; ++ i) {
for (int j = 0; j < n; ++ j) {
if (chessArr[i][j] != 0) {
sparseArray[++ count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chessArr[i][j];
}
}
}
将稀疏数组存入文件
- 这里使用字符缓冲流
BufferedWriter
。缓冲流 :能够高效读写的缓冲流,在基本流的对象基础上创建而来的,是相对于基本流的一种增强。 - 字符缓冲输出流 :其中有一个特有的成员方法
newLine()
可以在行末写入一个行分割符,根据不同的操作系统获取到不同的行分割符。
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("sparseArray.txt"));
for (int i = 0; i < sparseArray.length; ++ i) {
bw.write(sparseArray[i][0] + " " + sparseArray[i][1] + " " + sparseArray[i][2]);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
从文件中读出稀疏数组
- 字符缓冲输入流 :其中有一个特有的成员方法
readLine()
读取一行数据,返回值包含该行内容的字符串,不包含行终止符,如果已经到达流末尾,则返回null
。
在这里我们获取到的每一行数据以后,使用 作为分割符,将每一行数据都分割成一个一个的数据,保存在一个临时的数组中,方便我们后面对这个稀疏数组进程处理。
int cnt1 = 0;
String sparseArray1[][] = new String[1024][3];
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("sparseArray.txt"));
String line;
while ((line = br.readLine()) != null) {
sparseArray1[cnt1 ++] = line.split(" ");
System.out.println(sparseArray1[cnt1 - 1][0] + " " + sparseArray1[cnt1 - 1][1] + " " + sparseArray1[cnt1 - 1][2]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
将稀疏数组转化为二维数组
根据稀疏数组的第一行数据,我们可以创建出一个二维数组,然后在读取稀疏数组的最后几行,赋值给创建出来的二维数组就可以进行数据的复原
int chessArr1[][] = new int[Integer.parseInt(sparseArray1[0][0])][Integer.parseInt(sparseArray1[0][1])];
for (int i = 1; i <= Integer.parseInt(sparseArray1[0][2]); ++ i) {
chessArr1[Integer.parseInt(sparseArray1[i][0])][Integer.parseInt(sparseArray1[i][1])] = Integer.parseInt(sparseArray1[i][2]);
}
for (int i = 0; i < chessArr.length; ++ i) {
for (int j = 0; j < chessArr1[0].length; ++ j) {
System.out.print(chessArr1[i][j] + " ");
}
System.out.println();
}
稀疏数组源码
import java.io.*;
public class SparseArray {
public static void main(String[] args) {
int chessArr[][] = new int[11][11];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
chessArr[3][4] = 3;
// for (int[] ints : chessArr) {
// for (int anInt : ints) {
// System.out.print(anInt + " ");
// }
// System.out.println();
// }
int m = chessArr.length, n = chessArr[0].length, cnt = 0;
for (int i = 0; i < m; ++ i) {
for (int j = 0; j < n; ++ j) {
if (chessArr[i][j] != 0) ++ cnt;
}
}
// System.out.println(cnt);
int sparseArray[][] = new int[cnt + 1][3];
sparseArray[0][0] = m;
sparseArray[0][1] = n;
sparseArray[0][2] = cnt;
int count = 0;
for (int i = 0; i < m; ++ i) {
for (int j = 0; j < n; ++ j) {
if (chessArr[i][j] != 0) {
sparseArray[++ count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chessArr[i][j];
}
}
}
// for (int i = 0; i < sparseArray.length; ++ i) {
// for (int j = 0; j < 3; ++ j) {
// System.out.print(sparseArray[i][j] + " ");
// }
// System.out.println();
// }
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("sparseArray.txt"));
for (int i = 0; i < sparseArray.length; ++ i) {
bw.write(sparseArray[i][0] + " " + sparseArray[i][1] + " " + sparseArray[i][2]);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
int cnt1 = 0;
String sparseArray1[][] = new String[1024][3];
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("sparseArray.txt"));
String line;
while ((line = br.readLine()) != null) {
sparseArray1[cnt1 ++] = line.split(" ");
System.out.println(sparseArray1[cnt1 - 1][0] + " " + sparseArray1[cnt1 - 1][1] + " " + sparseArray1[cnt1 - 1][2]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
int chessArr1[][] = new int[Integer.parseInt(sparseArray1[0][0])][Integer.parseInt(sparseArray1[0][1])];
for (int i = 1; i <= Integer.parseInt(sparseArray1[0][2]); ++ i) {
chessArr1[Integer.parseInt(sparseArray1[i][0])][Integer.parseInt(sparseArray1[i][1])] = Integer.parseInt(sparseArray1[i][2]);
}
for (int i = 0; i < chessArr.length; ++ i) {
for (int j = 0; j < chessArr1[0].length; ++ j) {
System.out.print(chessArr1[i][j] + " ");
}
System.out.println();
}
// int chessArr2[][] = new int[sparseArray[0][0]][sparseArray[0][1]];
// for (int i = 1; i < sparseArray.length; ++ i) {
// chessArr2[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
// }
//
// for (int i = 0; i < chessArr2.length; ++ i) {
// for (int j = 0; j < chessArr2[0].length; ++ j) {
// System.out.print(chessArr2[i][j] + " ");
// }
// System.out.println();
// }
}
}