基本介绍
当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。
稀疏数组的处理方法是:
- 记录数组一共有几行几列,有多少个不同的值
- 把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
图解例子:
思路分析及代码实现
package com.atguigu.sparsearray;
import java.io.*;
import java.util.ArrayList;
/**
*
* @author: mayu
* @version: 1.0
* @date: 2019/6/4
* @time: 10:01
* @description: 稀疏数组
* (1):应用场景:编写一个五子棋,具有存盘退出和续上盘功能。
* (2):思路分析
* (3):代码实现
*/
public class SparseArray {
public static void main(String[] args) {
//1.初始化二维数组(11*11),0表示无效数据,1表示黑子,2表示蓝子
int chessArr[][] = new int[11][11];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
chessArr[5][6] = 1;
//2.遍历二维数组得到有效数据的个数
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr[i][j] != 0) {
sum++;
}
}
}
System.out.println("sum = " + sum);
//3.创建稀疏数组并赋值
int sparseArray[][] = new int[sum + 1][3];
sparseArray[0][0] = 11;
sparseArray[0][1] = 11;
sparseArray[0][2] = sum;
//记录是第几个有效数据
int count = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr[i][j] != 0) {
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chessArr[i][j];
}
}
}
//4.输出稀疏数组的形式
System.out.println("输出的稀疏数组如下:");
for (int i = 0; i < sparseArray.length; i++) {
System.out.printf("%d\t%d\t%d\t\n", sparseArray[i][0], sparseArray[i][1], sparseArray[i][2]);
}
System.out.println();
/**********************************************分割线*********************************************/
/*********************稀疏数组写入磁盘**********************/
try {
//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:\\sparseArray.txt"))));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\sparseArray.txt"));
for (int i = 0; i < sparseArray.length; i++) {
bw.write(sparseArray[i][0] + "," + sparseArray[i][1] + "," + sparseArray[i][2]);
bw.newLine();
}
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
/*********************从磁盘读取稀疏数组**********************/
ArrayList<String> list = new ArrayList();
try {
//BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\sparseArray.txt"))));
BufferedReader br = new BufferedReader(new FileReader("D:\\sparseArray.txt"));
String str = "";
while ((str = br.readLine()) != null) {
list.add(str);
System.out.println("读取内容为:" + str);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
//将磁盘读取的内容转换为稀疏数组
int sparseArray2[][] = new int[list.size()][3];
for (int i = 0; i < list.size(); i++) {
String[] split = list.get(i).split(",");
sparseArray2[i][0] = Integer.parseInt(split[0]);
sparseArray2[i][1] = Integer.parseInt(split[1]);
sparseArray2[i][2] = Integer.parseInt(split[2]);
}
System.out.println("将从磁盘读取的内容转换为稀疏数组为:");
for (int[] row : sparseArray2
) {
for (int data : row
) {
System.out.printf("%d\t", data);
}
System.out.println();
}
/**********************************************分割线*********************************************/
//5.将稀疏数组恢复成二维数组
//5.1 读取稀疏数组的第一行,初始化原来的二维数组
int chessArr1[][] = new int[sparseArray[0][0]][sparseArray[0][1]];
//5.2 读取稀疏数组的其余行,将有效数据赋值给二维数组
for (int i = 1; i < sparseArray.length; i++) {
chessArr1[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
}
//5.3 输出恢复后的二维数组
System.out.println("输出恢复后的二维数组:");
for (int[] row : chessArr1
) {
for (int data : row
) {
System.out.printf("%d\t", data);
}
System.out.println();
}
}
}