稀疏数组
当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。
稀疏数组的处理方法是:
- 记录数组一共有几行几列,有多少个不同的值
- 把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
应用实例
- 使用稀疏数组,来保留类似前面的二维数组(棋盘、地图等等)
- 把稀疏数组存盘,并且可以从新恢复原来的二维数组数
代码:
import sun.security.util.Length;
import java.io.*;
import java.util.ArrayList;
/**
* 棋盘用二维数组(11行,11列)表示,0为空,1为黑子,2为白子
* 将二维数组转换为稀疏数组,再存入磁盘。
* 从磁盘读取,将稀疏数组转换为二维数组显示。
*/
public class SparseArray {
public static void main(String[] args) throws IOException {
//棋盘用二维数组表示
int[][] chess = new int[11][11];
chess[1][2] = 1;
chess[2][3] = 2;
chess[3][4] = 1;
chess[5][6] = 2;
int[][] sparse = chessToSparse(chess);
printArr(sparse);
System.out.println("=====================分隔符===========================");
int[][] chess1 = sprseToChess(sparse);
printArr(chess1);
output(sparse);
int[][] input = input();
System.out.println("=====================分隔符===========================");
printArr(input);
}
/**
* 将二维数组输出到磁盘
* @param array
*/
public static void output(int[][] array) throws IOException {
File file = new File("D:/map.txt");
if (!file.exists()) {
file.createNewFile();
}
//为了避免乱码,用字符流输出数据
FileWriter fw = new FileWriter(file);
for (int[] row : array) {
for (int col : row) {
fw.write(String.valueOf(col) + "\n");
}
}
fw.close();
}
public static int[][] input() throws IOException {
File file = new File("D:/map.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
//用list数组保存读出的数据
ArrayList<String> list = new ArrayList<>();
String data;
while ((data = bufferedReader.readLine()) != null) {
list.add(data);
}
bufferedReader.close();
fileReader.close();
//初始化稀疏数组
int[][] array = new int[list.size() / 3][3];
int row = 0;
//循环集合将数据保存到稀疏数组
for (int i = 0; i < list.size(); ) {
array[row][0] = Integer.parseInt(list.get(i++));
array[row][1] = Integer.parseInt(list.get(i++));
array[row++][2] = Integer.parseInt(list.get(i++));
}
return array;
}
/**
* 将棋盘转换为稀疏数组返回
*
* @param chess
* @return
*/
public static int[][] chessToSparse(int[][] chess) {
int row = chess.length;
int col = chess[0].length;
//用list集合存入每个不为0点的横纵坐标和值
ArrayList<int[]> list = new ArrayList<>();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (chess[i][j] != 0) {
int[] data = new int[3];
data[0] = i;
data[1] = j;
data[2] = chess[i][j];
list.add(data);
}
}
}
//定义一个稀疏数组
//list.size 表示有效数据(非0)的个数
int[][] sparse = new int[list.size() + 1][3];
sparse[0][0] = row;
sparse[0][1] = col;
sparse[0][2] = list.size();
for (int i = 1; i < sparse.length; i++) {
sparse[i][0] = list.get(i - 1)[0];
sparse[i][1] = list.get(i - 1)[1];
sparse[i][2] = list.get(i - 1)[2];
}
return sparse;
}
/**
* 稀疏数组转换为棋盘
*
* @param sprse
* @return
*/
public static int[][] sprseToChess(int[][] sprse) {
//定义一个普通二维数组(棋盘),稀疏数组第一行的1、2列存的是原数组的几行几列
int[][] chess = new int[sprse[0][0]][sprse[0][1]];
//遍历稀疏数组,将有效值存入响应的行列中
for (int i = 1; i < sprse.length; i++) {
chess[sprse[i][0]][sprse[i][1]] = sprse[i][2];
}
return chess;
}
/**
* 打印二维数组
*
* @param array
*/
public static void printArr(int[][] array) {
for (int[] row : array) {
for (int col : row) {
System.out.printf("\t" + col);
}
System.out.println();
}
}
}
总结:
本人为刚毕业的学生,以上代码有不足之处大家可以多交流。
目前程序员的门槛越来越高,不管是面试还是之后的发展,数据结构和算法必不可少,只会CRUD的程序员将淘汰是趋势。以后这个账号会陆续记录学习笔记,加油,奥里给!