1.稀疏数组的应用场景
当二维数组或者多维数组中的数据大多数为无效的数据时,我们可以将二维数组转换为稀疏数组,这样我们在我们可以大大缩减存储二维数组所需要的空间。
例如下图所示的数组,0是数组创建时赋的默认值,只有两个有效的数据单元:
转成稀疏数组后:
这个稀疏数组是一个多行3列的数组,第一行表示二维数组的行,列以及二维数组中的有效数据的个数,剩下的行表示的是二维数组中的有效数据,第一列表示行,第二列表示列,第三列表示二维数组的值
2.实现(java)
实现将二维数组转化为稀疏数组,再将稀疏数组存储到文件中,
然后读取文件中的稀疏数组,再将其转化为二维数组。模拟了工作中的存储流程
package site.xuefeng.SparseArray;
import java.io.*;
/**
* @author feng
* @date 2020/5/22
* discription:*
*/
public class SparseArray {
public static void main(String[] args) {
//1表示白子2表示黑子
int [][] arr = new int [11][11];
arr[4][5] = 1;
arr[3][1] = 2;
//1.现将遍历数组的到有效数据的数量sum
System.out.println("原来的数组");
int sum = 0;
for (int[] col :arr){
for (int item:col){
System.out.printf("%d\t",item);
if (item!=0)
sum++;
}
System.out.println();
}
//2。根据sum建立稀疏数组 第一列表示行,第二列表示列,第三列表示数据
int [][] sparseArray = new int[sum+1][3];
sparseArray[0][0] = 11;
sparseArray[0][1] = 11;
sparseArray[0][2] = sum;
//3.再次遍历数组,构建稀疏数组
int count = 0;
for (int i = 0 ; i <arr.length; i++){
for (int j = 0 ; j < arr[i].length; j++){
if (arr[i][j]!=0){
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = arr[i][j];
}
}
}
File file = new File("src/site/xuefeng/file/file1.txt");
//将稀疏数组存入文件中
FileWriter writer = null;
try {
writer = new FileWriter(file);
BufferedWriter bufferedWriter= new BufferedWriter(writer);
//存入稀疏数组的行列数
bufferedWriter.write(sparseArray.length+"\t"+sparseArray[0].length+"\n");
for (int [] col:sparseArray){
bufferedWriter.write(col[0]+"\t"+col[1]+"\t"+col[2]+"\n");
}
bufferedWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if (writer!=null){
try {
writer.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
int [][] sparseArr = new int[1][1];
FileReader reader = null;
try {
reader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(reader);
String line = "";
line = bufferedReader.readLine();
String str[] = line.split("\t");
int col = Integer.valueOf(str[0]);
int row = Integer.valueOf(str[1]);
//构建稀疏数组
int num = 0;
sparseArr= new int[col][row];
while((line = bufferedReader.readLine())!=null){
String [] strs = line.split("\t");
sparseArr[num][0] = Integer.valueOf(strs[0]);
sparseArr[num][1] = Integer.valueOf(strs[1]);
sparseArr[num][2] = Integer.valueOf(strs[2]);
num++;
}
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if (reader!=null){
try {
reader.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
//输出文件中读取出稀疏数组
System.out.println("文件中读取出稀疏数组····");
for (int [] col:sparseArr){
System.out.printf("%d\t%d\t%d",col[0],col[1],col[2]);
System.out.println();
}
//转成二维数组
//根据稀疏数组创建二维数组
int [][] arr1 = new int[sparseArr[0][0]][sparseArr[0][1]];
//遍历稀疏数组构建二维数组
for (int i = 1 ; i <sparseArr.length; i++){
arr1[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
//输出二维数组
System.out.println("输出稀疏数组转化的二维数组111");
for (int[] col:arr1){
for (int item:col){
System.out.printf("%d\t",item);
}
System.out.println();
}
}
}
3.总结
在学习数据结构的同时,复习了java的io操作。