概念
当一个数组中
大部分元素为0,或者为
同一个值的数组时,可以使用稀疏数组来保存
原理
1.记录数组一共有几行几列,有多少个不同的值
2.把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
![](https://i-blog.csdnimg.cn/blog_migrate/36d62ef7e8ffdbe28e3ce7adb4ba1375.png)
方法
二维数组转稀疏数组
1. 遍历 原始的二维数组,得到有效数据的个数sum
2. 根据sum 就可以创建稀疏数组 sparseArr int[sum + 1] [3]
3. 将二维数组的有效数据数据存入到稀疏数组
稀疏数组转原始的二维数组的思路
1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
2. 在读取稀疏数组后几行的数据,并赋给原始的二维数组即可
public class SparseArray {
public static void main(String[] args) throws IOException {
//创建一个原始的二维数组11*11
//0:表示没有棋子,1:表示黑子,2:表示白子
int chessArr1[][] = new int[11][11];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
chessArr1[4][5] = 2;
//输出原始的二维数组
for (int[] row: chessArr1) {
for (int data: row) {
System.out.print("\t"+data);
}
System.out.println();
}
//将二维数组转稀疏数组
//1.先遍历二维数组得到非0数据的个数
int sum = 0;
for (int i = 0; i < chessArr1.length; i++) {
for (int j = 0; j < chessArr1.length; j++) {
if (chessArr1[i][j] != 0 ){
sum++;
}
}
}
System.out.println("sum="+sum);
//2.创建对应的稀疏数组
int sparseArr[][] = new int[sum+1][3];
//给稀疏数组赋值
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = sum;
//遍历二维数组,将非0的值存放到sparseArr中
//count用于记录是第几个非0数
int count = 0;
for (int i = 0; i < chessArr1.length; i++) {
for (int j = 0; j < chessArr1.length; j++) {
if (chessArr1[i][j] != 0 ){
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr1[i][j];
}
}
}
//将稀疏数组恢复成原始的二维数组
//1.先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
//2.再读取稀疏数组后几行的数据(从第二行开始),并赋给原始的二维数组即可
for (int i = 1; i < sparseArr.length; i++) {
chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
System.out.println("输出恢复后的二维数组:");
for (int[] row: chessArr2) {
for (int data : row) {
System.out.print(data+"\t");
}
System.out.println();
}
//稀疏数组创建后将其存入磁盘
File file = new File("E:\\map.txt");
if (!file.exists()){
file.createNewFile();
}
BufferedOutputStream bos = null;
try{
bos = new BufferedOutputStream(new FileOutputStream(file));
for (int i = 0; i < sparseArr.length; i++) {
String res = sparseArr[i][0] + "\t" + sparseArr[i][1] + "\t" + sparseArr[i][2] + "\n";
bos.write(res.getBytes(StandardCharsets.UTF_8));
}
System.out.println("保存成功");
}catch (Exception e){
e.printStackTrace();
}finally {
try {
bos.close();
}catch (Exception e){
e.printStackTrace();
}
}
//从磁盘中读取数据,并还原数组
BufferedReader br = new BufferedReader(new FileReader(file));
int[][] chessArr3 = null;
int row = 0;
String s = null;
try {
while ((s=br.readLine()) != null){
String[] split = s.split("\t");
System.out.println(Arrays.toString(split));
Integer i1 = Integer.valueOf(split[0]);
Integer i2 = Integer.valueOf(split[1]);
Integer i3 = Integer.valueOf(split[2]);
if (row == 0){
chessArr3 = new int[i1][i2];
row++;
}else{
chessArr3[i1][i2] = i3;
row++;
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
br.close();
}catch (Exception e){
e.printStackTrace();
}
}
System.out.println("恢复后的二维数组为:");
for (int[] a : chessArr3) {
for (int b : a){
System.out.print(b+"\t");
}
System.out.println();
}
}
}