介绍
当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。 稀疏数组的处理方法是:
- 记录数组一共有几行几列,有多少个不同的值
- 把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
举例
-
五子棋实现:转化为二维数组->转化为稀疏数组->把稀疏数组存入本地文件(存档)
-
二维数组 转 稀疏数组的思路
- 遍历 原始的二维数组,得到有效数据的个数 sum
- 根据sum 就可以创建 稀疏数组 sparseArr int
[sum + 1][3] - 将二维数组的有效数据数据存入到 稀疏数组
- 代码实现如下:
/**
* 把二维数组转化成稀疏数组
* @param chessArr
* @return
*/
public int[][] getSparseArr(int [][]chessArr){
for (int[] ints : chessArr) {
for (int anInt : ints) {
System.out.printf("%d\t",anInt);
}
System.out.println();
}
//-----------二维数组->稀疏数组------------
//先统计二维数组中不为0的值,把值赋给sum
int sum = 0;
for (int i=0;i<chessArr.length;i++){
for (int j=0;j<chessArr[i].length;j++){
if (chessArr[i][j]!=0){
sum++;
}
}
}
//创建相应的稀疏数组,把稀疏数组的第一行填充好(分别为二维数组的行数,列数,以及不为0的宿数组元素)
int sparseArr[][]=new int[sum+1][3];
sparseArr[0][0]=chessArr.length;
sparseArr[0][1]=chessArr[0].length;
sparseArr[0][2]=sum;
//使用count记录稀疏数组的空行位置(0),再遍历二维数组把二维数组中的不为0的值的行数列数以及值填充到二维数组中
int count=1;
for (int i=0;i<chessArr.length;i++){
for (int j=0;j<chessArr[i].length;j++){
if (chessArr[i][j]!=0){
if (sparseArr[count][0]==0){
sparseArr[count][0]=i;
sparseArr[count][1]=j;
sparseArr[count][2]=chessArr[i][j];
count++;
}
}
}
}
System.out.println("------------------------->");
for (int[] ints : sparseArr) {
for (int anInt : ints) {
System.out.printf("%d\t",anInt);
}
System.out.println();
}
return sparseArr;
}
- 稀疏数组转原始的二维数组的思路
- 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的 chessArr2 = int
[11][11] - 在读取稀疏数组后几行的数据,并赋给 原始的二维数组 即可.
- 代码实现如下:
- 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的 chessArr2 = int
/**
* 把稀疏数组转化成二维数组
* @param sparseArr
* @return
*/
public int[][] getarr(int [][]sparseArr){
//-----------稀疏数组->二维数组------------
//根据稀疏数组创建好二维数组
int chessArr[][]=new int[sparseArr[0][0]][sparseArr[0][1]];
for (int i=1;i<sparseArr.length;i++) {
chessArr[sparseArr[i][0]][sparseArr[i][1]]=sparseArr[i][2];
}
System.out.println("------------------------>");
for (int[] ints : chessArr) {
for (int anInt : ints) {
System.out.printf("%d\t",anInt);
}
System.out.println();
}
return chessArr;
}
- 把稀疏数组存到本地文件中:
//把稀疏数组输出到文件
public static void load(int[][] sparseArr){
FileWriter fw =null;
try {
File file = new File("map.txt");
if (!file.exists()){
file.createNewFile();
}
fw = new FileWriter(file);
for (int i = 0; i < sparseArr.length; i++) {
for (int j = 0; j < sparseArr[i].length; j++) {
fw.write(sparseArr[i][j]+"\t");
}
fw.write("\r\n");
}
} catch (IOException ioException) {
ioException.printStackTrace();
}finally{
if (fw!=null){
try {
fw.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
}
- 读取文件中的稀疏数组到application中
//把稀疏数组存放的读取
public static int[][] downLoad(){
BufferedReader bf =null;
try {
File file = new File("D:\Workplace_java\数据结构与算法\map.txt");
FileReader fr = new FileReader(file);
bf = new BufferedReader(fr);
String line;
List<Integer> list = new ArrayList<>();
while ((line=bf.readLine())!=null){
String[] temp = line.split("\t");
for (int i =0 ;i<temp.length;i++) {
list.add(Integer.parseInt(temp[i]));
}
}
int[][] sparseArr=new int[list.size()/3][3];
Object[] integers =list.toArray();
for (int i=0;i<sparseArr.length;i++){
for (int j=0;j<sparseArr[i].length;j++){
sparseArr[i][j]= (int) integers[i*3+j];
}
}
return sparseArr;
} catch (Exception e) {
e.printStackTrace();
try {
if (bf!=null){
bf.close();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
return null;
}