import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SparseArray {
public static void main(String[] args) {
// 保存的磁盘文件目录。
String fileName = "D:\\sparseArray.txt";
//创建大小11*11棋盘。
int chessDefault = 11;
int[][] chessArray = new int[chessDefault][chessDefault];
//创建3个二维数组的非0数据表示旗子,1为黑子,2为蓝子,3为红子。
chessArray[1][2] = 1;
chessArray[2][3] = 2;
chessArray[4][5] = 3;
//输出原标准二维数组
System.out.println("输出原二维数组----:");
SparseArray.printArray(chessArray);
//转换为稀疏数组并输出
int[][] sparseArray = SparseArray.toSparseArray(chessArray, chessDefault);
System.out.println("输出转换的稀疏数组----:");
SparseArray.printArray(sparseArray);
//存储二维数组到磁盘
System.out.println("存数稀疏数组到磁盘----:");
SparseArray.writeArraytoDisk(fileName, sparseArray);
System.out.println("读取稀疏数组到内存----:");
int[][] listArray = new int[0][];
try {
listArray = readArrayFromdisk(fileName);
} catch (IOException e) {
e.printStackTrace();
}
SparseArray.printArray(listArray);
System.out.println("稀疏数组还原初始二维数组----:");
int[][] sourceArray = new int[listArray[0][0]][listArray[0][1]];
for (int i = 1; i < listArray.length; i++) {
sourceArray[listArray[i][0]][listArray[i][1]] = listArray[i][2];
}
System.out.println("6.原数组输出----:");
printArray(sourceArray);
}
/*
读取稀疏数组文件
*/
private static int[][] readArrayFromdisk(String fileName) throws IOException {
//未知文件 几行几列
int row = 0, col = 0;
List<int[]> readList = new ArrayList<>();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName))));
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
//读取的String 转换为int数组存数在ArrayList中
int[] array = Arrays.stream(lineTxt.split("\\t")).mapToInt(Integer::parseInt).toArray();
System.out.println("读取稀疏数组----:" + Arrays.toString(array));
readList.add(array);
col = array.length;
}
row = readList.size();
bufferedReader.close();
// ArrayList内部一维数组转化为二维数组
int[][] readArray = new int[row][col];
for (int i = 0; i < readArray.length; i++) {
readArray[i] = readList.get(i);
}
return readArray;
}
/*
存储数组到磁盘
*/
private static void writeArraytoDisk(String fileName, int[][] sparseArray) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(new File(fileName), false));
} catch (IOException e) {
e.printStackTrace();
}
StringBuffer sb = new StringBuffer();
for (int[] row : sparseArray) {
for (int i : row) {
sb.append(i + "\t");
}
sb.append("\r\n");
}
try {
bw.write(sb.toString());
bw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
转换稀疏数组方法
*/
private static int[][] toSparseArray(int[][] chessArray, int chessDefault) {
//转化为3列稀疏数组 有效数据sum++
int sum = 0;
for (int i = 0; i < chessArray.length; i++) {
for (int j = 0; j < chessArray[i].length; j++) {
if (chessArray[i][j] != 0) {
sum++;
}
}
}
//创建稀疏数组 3列 有效数据+1行 第0行为数组基本信息
int[][] sparseArray = new int[sum + 1][3];
sparseArray[0][0] = chessDefault;
sparseArray[0][1] = chessDefault;
sparseArray[0][2] = sum;
//第1行开始放数据
int count = 1;
for (int i = 0; i < chessArray.length; i++) {
for (int j = 0; j < chessArray[i].length; j++) {
if (chessArray[i][j] != 0) {
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chessArray[i][j];
count++;
}
}
}
return sparseArray;
}
/*
二维数组标准输出
*/
public static void printArray(int[][] twoDimensionalArray) {
System.out.println("二维数组标准输出---begin");
for (int[] row : twoDimensionalArray) {
for (int i : row) {
System.out.printf("%d\t", i);
}
System.out.println();
}
System.out.println("二维数组标准输出---end");
}
}
datastructure:稀疏数组存盘
最新推荐文章于 2022-10-15 10:16:23 发布