稀疏数组可以简单的看作为是压缩,在开发中也会使用到。比如将数据序列化到磁盘上,减少数据量,在IO过程中提高效率等等。
本案例使用的是两种版本,分别是java和scala,具体实现其实大同小异。废话不多说,直接上案例,希望大家能过通过这个简单的案例对稀疏数组有一个更好的理解
scala版本
import java.io._
import scala.collection.mutable.ArrayBuffer
/**
* @author zach - 吸柒
* Scala版本 数据结构 - 稀疏数组
*/
object SparseArray {
def main(args: Array[String]): Unit = {
val rowSize = 11
val colSize = 11
//定义一个11行11列的二维数组
val chessMap = Array.ofDim[Int](rowSize, colSize)
chessMap(1)(2) = 1 // 1表示黑子
chessMap(2)(3) = 2 // 2表示白子
for (elem <- chessMap) {
for (elem2 <- elem) {
printf("%d\t", elem2)
}
println()
}
// 稀疏数组的实现
val sparseArray = ArrayBuffer[Data]()
//定义第一行 数据为 行列数
sparseArray.append(new Data(rowSize, colSize, 0))
//遍历棋盘
for (i <- 0 until chessMap.length) {
for (j <- 0 until chessMap(i).length) {
//判断棋盘上是否有子,不为0将数值保存到数组中
if (chessMap(i)(j) != 0) {
sparseArray.append(new Data(i, j, chessMap(i)(j)))
}
}
}
//展示稀疏后的数组
println("-------------稀疏数组展示--------------")
println("行\t列\t值")
for (elem <- sparseArray) {
printf("%d\t%d\t%d\n", elem.row, elem.col, elem.value)
}
//定义路径
val path: File = new File("d:/data2.txt")
//写出
val oos: ObjectOutputStream = new ObjectOutputStream(new FileOutputStream(path))
oos.writeObject(sparseArray)
oos.flush()
oos.close()
//读取
val ois: ObjectInputStream = new ObjectInputStream(new FileInputStream(path))
//将读取的数据转换成可变Array数组
val sparseArray2: ArrayBuffer[Data] = ois.readObject().asInstanceOf[ArrayBuffer[Data]]
ois.close()
//恢复原始数据
val newData = sparseArray2(0) // 取出第一条数据
val rowSize2 = newData.row
val colSize2 = newData.col
//创建出原始棋盘的大小
val chessMap2 = Array.ofDim[Int](rowSize2, colSize2)
for (i <- 1 until sparseArray2.length) {
val data = sparseArray2(i)
//将数据恢复到新的棋盘上
chessMap2(data.row)(data.col) = data.value
}
println("-------------稀疏数组恢复原始数据展示--------------")
for (elem <- chessMap2) {
for (elem2 <- elem) {
printf("%d\t", elem2)
}
println()
}
}
}
class Data(val row: Int, val col: Int, val value: Int) extends Serializable
提示:这里的输出输入都是在同一个类中实现的,有兴趣的朋友可以编写两个不同的类,一个类将数据稀疏后输出到磁盘,在到另外一个类中恢复成原来的数据,体验一把。
java版本
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author zach - 吸柒
* Java版本 数据结构 - 稀疏数组
*/
public class SparseArrayFromJava {
public static void main(String[] args) {
int rowSize = 11;
int colSize = 11;
//定义一个11行11列的二维数组 类型为Integer
int[][] chessMap = new int[rowSize][colSize];
chessMap[1][2] = 1;
chessMap[2][3] = 2;
//输出初始棋盘
for (int[] integers : chessMap) {
for (int integer : integers) {
System.out.print(integer + "\t");
}
//换行
System.out.println();
}
// 稀疏数组的实现
List<ChessData> sparseList = new ArrayList<ChessData>();
//将原始数据写入数组第一行
sparseList.add(new ChessData(rowSize, colSize, 0));
//遍历棋盘
for (int i = 0; i < chessMap.length; i++) {
for (int j = 0; j < chessMap[i].length; j++) {
//判断棋盘上是否有子,不为0将数值保存到数组中
if (chessMap[i][j] != 0) {
sparseList.add(new ChessData(i, j, chessMap[i][j]));
}
}
}
System.out.println("-------------稀疏数组展示--------------");
for (ChessData chessData : sparseList) {
System.out.println(chessData);
}
//写出数据
File path = new File("d:/data.txt");
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(path));
oos.writeObject(sparseList);
oos.flush();
oos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//读取数据
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(path));
List<ChessData> sparseList2 = (List<ChessData>) ois.readObject();
//恢复原始数据
ChessData data = sparseList2.get(0);
int rowSize2 = data.row;
int colSize2 = data.col;
//恢复原始棋盘的大小
int[][] chessMap2 = new int[rowSize2][colSize2];
for (int i = 1; i < sparseList2.size(); i++) {
//获得有值的数据,恢复数据
ChessData cd = sparseList2.get(i);
chessMap2[cd.row][cd.col] = cd.value;
}
System.out.println("-------------稀疏数组恢复原始数据展示--------------");
for (int[] integers : chessMap2) {
for (int integer : integers) {
System.out.print(integer + "\t");
}
//换行
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class ChessData implements Serializable {
int row;
int col;
int value;
public ChessData(int row, int col, int value) {
this.row = row;
this.col = col;
this.value = value;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public String toString() {
return "row=" + row + "\t col=" + col + "\t value=" + value;
}
}
其实java版本与scala版本是一样的,主要是编写的语言不同,可以供大家做一个参考。