稀疏数组和二维数组转换
稀疏数组:当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组
稀疏数组的处理方法:
- 记录数组一共有多少行,有多少个不同的值
- 把具有不同值得元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
应用:
- 保存二维数组(棋盘、地图)
- 二维数组 ——> 稀疏数组的思路
- 遍历原始二维数组,得到有效数据的个数sum
- 根据sum创建稀疏数组
SparseArr int[sum + 1][3]
- 将二维数组的有效数据存入到稀疏数组
- 稀疏数组 ——> 二维数组
- 读取稀疏数组的第一行,根据第一行的数据,创建原始二维数组,比如上面的
chessArr2 = int[11][11]
- 在读取稀疏数组后几行的数据,并赋给原始的二维数组即可
- 读取稀疏数组的第一行,根据第一行的数据,创建原始二维数组,比如上面的
package com.starking.sparseArr;
/**
* @author Manix
* 稀疏数组和原始二维数组的相互转换写入文件,保存和读取
*/
public class SparseArray {
public static void main(String[] args) {
//创建一个二维数组 11*11
//0:没有棋子,1:黑子, 2:蓝子
int[][] chessArr1 = new int[11][11];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
//输出原始二维数组
System.out.println("原始的二维数组为:");
for (int[] row : chessArr1) {
for (int item : row) {
System.out.printf("%d\t", item);
}
System.out.println();
}
//将二维数组 转 稀疏数组的思路
//先遍历二维数组,得到非0个元素个数
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; 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;
// 给稀疏数组赋值
int count = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr1[i][j] != 0) {
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr1[i][j];
}
}
}
//输出稀疏数组
System.out.println();
System.out.println("原始二维数组----》---稀疏数组:");
for (int i = 0; i < sparseArr.length; i++) {
System.out.printf("%d\t%d\t%d\t\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
}
//将稀疏数组----》---恢复成原始的二维数组
int[][] chessArr2 = new int[sparseArr[0][0]][sparseArr[0][1]];
for (int i = 1; i < sparseArr.length; i++) {
chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
System.out.println();
//输出恢复之后的二维数组
System.out.println("稀疏数组----》---恢复的二维数组:");
for (int[] row : chessArr2) {
for (int item : row) {
System.out.printf("%d\t", item);
}
System.out.println();
}
}
/**
* 添加保存稀疏数组和从本地文件读取稀疏数组的功能
*/
// 创建内部类---保存和读取稀疏数组工具类
public static class SaveAndReadUtil {
// 写入文件的方法
public static void saveArray(int[][] array, String path) {
// 创建文件输入流
FileWriter fw = null;
try {
// 写入的文件路径及文件名
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
//把文件写入到字节流中
fw = new FileWriter(file);
//循环遍历数组写入
for (int[] ints : array) {
for (int j = 0; j < array.length - 1; j++) {
fw.write(ints[j] + "\t");
}
//数组的最后一行不加"\t"
fw.write(ints[array[0].length - 1] + "");
fw.write("\n");
}
//刷新流
fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//如果fw不为空,就将其关闭
if (fw != null) {
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//读取数组文件的方法
public static int[][] readArray(String path) {
//声明字符输入流
FileReader fr = null;
//声明字符输入缓冲流
BufferedReader br = null;
//定义一个二维数组
int[][] sparseArr0 = null;
try {
fr = new FileReader(path);
//通过BufferReader包装字符输入流
br = new BufferedReader(fr);
//穿件一个集合用来存读取的文件数据
List<String> list = new ArrayList<>();
//用来存放一行的数据
String lineStr;
//逐行读取文件内容
while ((lineStr = br.readLine()) != null) {
list.add(lineStr);
}
//获取文件的行数
int lineNum = list.size();
//获取文件列数
String s = list.get(0);
int columnNum = s.split("\t").length;
//根据文件行数和列数创建对应的数组
sparseArr0 = new int[list.size()][columnNum];
//记录输出当前行数
int count = 0;
//遍历集合,将集合中的数据存入数组中
for (String str : list) {
//读取str按照“\t”分割,用字符串数组接收
String[] strs = str.split("\t");
for (int i = 0; i < columnNum; i++) {
sparseArr0[count][i] = Integer.parseInt(strs[i]);
}
count++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭字符输入流
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//返回稀疏数组
return sparseArr0;
}
}
}