标题数据结构与算法(java版)之稀疏数组
1数据结构和算法的关系
1.1 数据 data 结构(structure)是一门研究组织数据方式的学科,有了编程语言也就有了数据结构.学好数据结构可以编写出更加漂亮,更加有效率的代码。
1.2 程序 = 构 数据结构+ 算法。
1.3 数据结构是算法的基础, 换言之,想要学好算法,需要把数据结构学到位。
2 数据结构包括:线性结构和非线性结构。
线性结构:数据元素之间存在一对一的线性关系,存在两种不同的存储结构,即顺序存储结构(数组)和链式存储结构(链表)----存储空间是否连续。注意:链表存储空间不一定连续,而数据存储空间是连续的。
非线性结构:二维数组,多维数组,广义表,树结构,图结构等
3 稀疏数组
3.1 实际需求
例如五子棋程序中,有存盘和续上盘的功能,有的时候会存储很多无用的数据,此时可以使用稀疏数组来压缩空间。
如果二维数组囧很多值的默认值为0,或者为同一个值的数组的时候,可以使用稀疏数组来存储。
处理方式===
1)第一行:数组有几行,几列,多少特殊值
2)将每个特殊值的行,列,值信息保存在不同的行中
实际应用:
1)使用稀疏数组保存二维数组(棋盘,地图等)
2)把稀疏数组进行存盘,并可以恢复。
业务需求:
1)将一个数组转化为稀疏数组,并保存到磁盘中去。
2)将磁盘中的文件读取出来,并转换为原数组。
图解:
具体代码实现
/**
* @author 龍
*/
public class SparseArray {
public static void main(String[] args) {
//创建一个原始的二维数组 11*11
int[][] chessArray1=new int[11][11];
//给有效数据赋值
chessArray1[1][2]=1;
chessArray1[2][3]=2;
chessArray1[4][5]=2;
//输出原始的二维数组
System.out.println("原始数组的数据为:");
for (int[] ints : chessArray1) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
//将二维数组转换为稀疏数组
//1 遍历二维数组,获取非零数据的个数
int sum=0;
for (int i = 0; i < chessArray1.length; i++) {
for (int j = 0; j < chessArray1[i].length; j++) {
if (chessArray1[i][j]!=0){
sum++;
}
}
}
//2 给稀疏数组初始化
int[][] sparseArray=new int[sum+1][3];
sparseArray[0][0]=chessArray1.length;
sparseArray[0][1]=chessArray1[0].length;
sparseArray[0][2]=sum;
//3 遍历二维数组,将非零的值存放到稀疏数组中去
int count=0;
for (int i = 0; i < chessArray1.length; i++) {
for (int j = 0; j < chessArray1[i].length; j++) {
if (chessArray1[i][j]!=0){
count++;
sparseArray[count][0]=i;
sparseArray[count][1]=j;
sparseArray[count][2]=chessArray1[i][j];
}
}
}
//4 遍历稀疏数组
System.out.println("稀疏数组的形式为:");
for (int i = 0; i < sparseArray.length; i++) {
for (int j = 0; j < sparseArray[i].length; j++) {
System.out.print(sparseArray[i][j]+"\t");
}
System.out.println();
}
//将稀疏数组保存到文件中去,使用io的方式
File file = new File("E:\\mydemo\\metadata\\metadata.txt");
try {
if (file.exists()){
file.delete();
}
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file,true);
for (int i = 0; i < sparseArray.length; i++) {
for (int j = 0; j < sparseArray[i].length; j++) {
fileOutputStream.write(sparseArray[i][j]);
}
}
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//将稀疏数组从文件种读取出来
ArrayList<Integer> arrayList = new ArrayList();
try {
FileInputStream fileInputStream = new FileInputStream(new File("E:\\mydemo\\metadata\\metadata.txt"));
int len;
while ((len=fileInputStream.read())!=-1){
arrayList.add(len);
}
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//将稀疏数组还原为之前的数组结构
//1 构建原来的二维数组
int[][] chessArray2=new int[arrayList.get(0)][arrayList.get(1)];
//2 读取稀疏数组的值放入到生成的数组种
/*for (int i = 1; i <sparseArray.length; i++) {
chessArray2[sparseArray[i][0]][sparseArray[i][1]]=sparseArray[i][2];
}*/
for (int i = 3; i < arrayList.size(); i=i+3) {
chessArray2[arrayList.get(i)][arrayList.get(i+1)]=arrayList.get(i+2);
}
System.out.println("恢复后的二维数组:");
for (int i = 0; i < chessArray2.length; i++) {
for (int j = 0; j < chessArray2[i].length; j++) {
System.out.print(chessArray2[i][j]+"\t");
}
System.out.println();
}
}
}
未完待续…