稀疏数组demo
import java.io.*;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
public class SparseArrayDemo {
public static void main(String[] args) {
//创建一个原始二维数组(10x10),模拟棋盘
int[][] array = new int[10][10];
//二维数组赋值(0:没有棋子,1:白棋,2:黑棋)
array[1][5] = 1;
array[5][2] = 2;
array[3][7] = 2;
//路径
String path = "data.txt";
System.out.println("---------------------");
System.out.println("原始数组:");
printArray(array);
System.out.println("---------------------");
System.out.println("数组转稀疏数组:");
int[][] sparseArray = arrayToSparseArray(array);
printArray(sparseArray);
System.out.println("---------------------");
System.out.println("稀疏数组保存到文件");
writeArray(sparseArray,path);
System.out.println("---------------------");
System.out.println("读取文件到稀疏数组");
int[][] sparseArrayFile = readArray(path);
printArray(sparseArrayFile);
System.out.println("---------------------");
System.out.println("稀疏数组转数组");
int[][] arrayFile = sparseArrayToArray(sparseArrayFile);
printArray(arrayFile);
System.out.println("---------------------");
}
public static int[][] arrayToSparseArray(int[][] array) {
//获取数组中有效值数量(!=0)
int count = 1;
for (int[] arr : array) {
for (int i : arr) {
if (i != 0) {
count++;
}
}
}
//创建稀疏数组
int[][] sparseArray = new int[count][3];
//向稀疏数组赋值
sparseArray[0][0] = array.length;
sparseArray[0][1] = array[0].length;
sparseArray[0][2] = count;
int x = 1;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (array[i][j] != 0) {
sparseArray[x][0] = i;
sparseArray[x][1] = j;
sparseArray[x][2] = array[i][j];
x++;
}
}
}
return sparseArray;
}
public static int[][] sparseArrayToArray(int[][] sparseArray) {
//创建数组
int[][] array = new int[sparseArray[0][0]][sparseArray[0][1]];
//向数组赋值
int x = 0;
int y = 0;
int z = 0;
for (int i = 1; i < sparseArray.length; i++) {
for (int j = 0; j < sparseArray[i].length; j++) {
switch (j) {
case 0:
x = sparseArray[i][j];
break;
case 1:
y = sparseArray[i][j];
break;
case 2:
z = sparseArray[i][j];
break;
}
}
array[x][y] = z;
}
return array;
}
public static void printArray(int[][] array) {
//遍历
for (int[] arr : array) {
for (int i : arr) {
System.out.printf(i + "t");
}
System.out.println();
}
}
public static void writeArray(int[][] array, String path) {
FileOutputStream fileOutputStream = null;
try {
File file = new File(path);
fileOutputStream = new FileOutputStream(file, true);
String value = "";
for (int[] arr : array) {
for (int i : arr) {
value += i + "t";
}
value += "n";
}
fileOutputStream.write(value.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭流
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
System.out.println("保存成功...");
}
public static int[][] readArray(String path) {
int[][] array = null;
File file = new File(path);
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
if (file.isFile() && file.exists()) {
try {
fileInputStream = new FileInputStream(file);
inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String lineTxt = null;
int x = 0;
while ((lineTxt = bufferedReader.readLine()) != null) {
String[] line = lineTxt.split("t");
if (x == 0) {
array = new int[Integer.parseInt(line[2])][3];
}
array[x][0] = Integer.parseInt(line[0]);
array[x][1] = Integer.parseInt(line[1]);
array[x][2] = Integer.parseInt(line[2]);
x++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭流
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
System.out.println("读取成功...");
} else {
System.out.println("文件不存在...");
}
return array;
}
}