package com.xch.sparsearray;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class SparseArrayReplay {
public static void main(String[] args) {
// 搞个二维数组
int[][] chessArr1 = generateTheSpecifiedTwoDimensionalArray(11, 11);
// 加些非0数字
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
chessArr1[4][5] = 1;
// 二维数组转化为稀疏数组
int[][] sparseArr1 = convertTwoDimensionalArrayToSparseArray(chessArr1);
System.out.println("稀疏数组");
for (int[] ints : sparseArr1) {
for (int anInt : ints) {
System.out.printf("%d\t", anInt);
}
System.out.println();
}
System.out.println();
// 将稀疏数组存入磁盘中
// System.out.println(saveSparseArrayToDisk(sparseArr1));
// 将稀疏数组从磁盘中拿出来
int[][] sparseArr2 = takeSparseArrayFromDisk();
System.out.println("从磁盘拿出来后的稀疏数组");
for (int[] ints : sparseArr2) {
for (int anInt : ints) {
System.out.printf("%d\t", anInt);
}
System.out.println();
}
System.out.println();
// 将稀疏数组转换为二维数组
System.out.println("将稀疏数组转换为二维数组");
int[][] chessArr2 = convertSparseArrayToTwoDimensionalArray(sparseArr2);
for (int[] ints : chessArr2) {
for (int anInt : ints) {
System.out.printf("%d\t", anInt);
}
System.out.println();
}
System.out.println();
}
// 生成指定的二维数组
public static int[][] generateTheSpecifiedTwoDimensionalArray(int num1, int num2) {
int[][] arr = new int[num1][num2];
return arr;
}
// 将二维数组转化为稀疏数组
public static int[][] convertTwoDimensionalArrayToSparseArray(int[][] arr) {
int rows = arr.length; // 获取二维数组的行数
int cols = arr[0].length; // 获取二维数组的列数
int nonZeroDigit = 0; // 非零数字
for (int[] ints : arr) {
for (int anInt : ints) {
if (anInt != 0) {
nonZeroDigit++;
}
}
}
int[][] sparseArr = new int[nonZeroDigit + 1][3]; // 用二维数组的有效数字定义稀疏数组的行, 列数为3(固定)
sparseArr[0][0] = rows; //二维数组的行
sparseArr[0][1] = cols; // 列
sparseArr[0][2] = nonZeroDigit; // 有效数字
int index = 1;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] != 0) {
sparseArr[index][0] = i;
sparseArr[index][1] = j;
sparseArr[index][2] = arr[i][j];
index++;
}
}
}
return sparseArr;
}
// 将稀疏数组存入磁盘中
public static Boolean saveSparseArrayToDisk(int[][] arr) {
File storagePath = new File("D:\\code\\dataStructure\\dataStructures\\sparseArrayReplay");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(storagePath));
for (int[] ints : arr) {
for (int anInt : ints) {
bw.write(anInt + "\t");
}
bw.write("\n");
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
// 将稀疏数组从磁盘中拿出来
public static int[][] takeSparseArrayFromDisk() {
File src = new File("D:\\code\\dataStructure\\dataStructures\\sparseArrayReplay");
BufferedReader br = null;
List<Integer> list = new ArrayList<>();
try {
br = new BufferedReader(new FileReader(src));
String str;
while ((str = br.readLine()) != null) {
String[] split = str.split("\t");
for (int i = 0; i < split.length; i++) {
list.add(Integer.parseInt(split[i]));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
int sparseArr[][] = new int[list.get(2) + 1][3];
int index = 0;
for (int i = 0; i < list.size(); i += 3) {
sparseArr[index][0] = list.get(i);
sparseArr[index][1] = list.get(i + 1);
sparseArr[index][2] = list.get(i + 2);
index++;
}
return sparseArr;
}
// 将稀疏数组转换为二维数组
public static int[][] convertSparseArrayToTwoDimensionalArray(int[][] arr) {
int chessArr[][] = new int[arr[0][0]][arr[0][1]];
for (int i = 1; i < arr.length; i++) {
chessArr[arr[i][0]][arr[i][1]] = arr[i][2];
}
return chessArr;
}
}