初始化二维数组
//初始化棋盘
int[][] data = new int[11][11];
data[4][5] = 1;
data[5][6] = 2;
data[0][1] = 2;
将二维数组转为稀疏数组
//转为稀松
public static int[][] toSparseArray(int[][] data) throws IOException {
//1统计 数据量
int count = 0;
for (int[] i : data) {
for (int i1 : i) {
if (i1 != 0)
count++;
}
}
//2根据数据量 创建稀松数组 并初始化首行 为原数组 行、列、数据量
int[][] sparseArray = new int[count + 1][3];
sparseArray[0][0] = data.length;
sparseArray[0][1] = data[0].length;
sparseArray[0][2] = count;
//3遍历数据源记录数据的 坐标
int row = 0;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
if (data[i][j] != 0) {
row = count--;
sparseArray[row][0] = i;
sparseArray[row][1] = j;
sparseArray[row][2] = data[i][j];
}
}
}
System.out.println("-------------sparseArray--------------");
return sparseArray;
}
将稀疏数组变回二维数组
//将稀疏数组转为原数据
public static int[][] bySparseArray(int[][] sparseArray) throws IOException, ClassNotFoundException {
int[][] back = new int[sparseArray[0][0]][sparseArray[0][1]];
for (int i = 1; i < sparseArray.length; i++) {
back[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
}
System.out.println("-------------back-----------------");
forEachOut(back);
return back;
}
加入 IO 读写
通过IO流实现文件写入
//通过 IO 输出到文件
String dir = new File("").getCanonicalPath();
String file = dir + "\\src\\main\\resources\\static\\file";
File path = new File(file);
if (!path.exists()){
boolean mkdirs = path.mkdirs();
}
FileOutputStream fos = new FileOutputStream(path+"/sparseArray1.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(sparseArray);
fos.close();
oos.close();
通过IO流读取文件
//通过 IO 读取稀松列表
String dir = new File("").getCanonicalPath();
String file = dir + "\\src\\main\\resources\\";
File path = new File(file + pathRelativeToResource);
FileInputStream fis = new FileInputStream(path);
ObjectInputStream ois = new ObjectInputStream(fis);
int[][] sparseArray = (int[][]) ois.readObject();
fis.close();
ois.close();
完整代码
import java.io.*;
public class sparseArray {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//初始化棋盘
int[][] data = new int[11][11];
data[4][5] = 1;
data[5][6] = 2;
data[0][1] = 2;
//遍历棋盘
forEachOut(data);
//转为稀松数组
toSparseArray(data);
//读取稀松
String path = "static/file/sparseArray1.txt";
int[][] ints = bySparseArray(path);
}
//遍历棋盘
public static void forEachOut(int[][] data) {
for (int[] datum : data) {
for (int i : datum) {
System.out.print(i + " ");
}
System.out.println();
}
}
//转为稀松
public static int[][] toSparseArray(int[][] data) throws IOException {
//1统计 数据量
int count = 0;
for (int[] i : data) {
for (int i1 : i) {
if (i1 != 0)
count++;
}
}
//2根据数据量 创建稀松数组 并初始化首行 为原数组 行、列、数据量
int[][] sparseArray = new int[count + 1][3];
sparseArray[0][0] = data.length;
sparseArray[0][1] = data[0].length;
sparseArray[0][2] = count;
//3遍历数据源记录数据的 坐标
int row = 0;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
if (data[i][j] != 0) {
row = count--;
sparseArray[row][0] = i;
sparseArray[row][1] = j;
sparseArray[row][2] = data[i][j];
}
}
}
System.out.println("-------------sparseArray--------------");
forEachOut(sparseArray);
//通过 IO 输出到文件
String dir = new File("").getCanonicalPath();
String file = dir + "\\src\\main\\resources\\static\\file";
File path = new File(file);
if (!path.exists()){
boolean mkdirs = path.mkdirs();
}
FileOutputStream fos = new FileOutputStream(path+"/sparseArray1.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(sparseArray);
fos.close();
oos.close();
return sparseArray;
}
//读取稀松
public static int[][] bySparseArray(String pathRelativeToResource) throws IOException, ClassNotFoundException {
//通过 IO 读取稀松列表
String dir = new File("").getCanonicalPath();
String file = dir + "\\src\\main\\resources\\";
File path = new File(file + pathRelativeToResource);
FileInputStream fis = new FileInputStream(path);
ObjectInputStream ois = new ObjectInputStream(fis);
int[][] sparseArray = (int[][]) ois.readObject();
fis.close();
ois.close();
//将稀疏数组转为原数据
int[][] back = new int[sparseArray[0][0]][sparseArray[0][1]];
for (int i = 1; i < sparseArray.length; i++) {
back[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
}
System.out.println("-------------back-----------------");
forEachOut(back);
return back;
}
}