稀疏数组实现棋盘存储与恢复
一、导入Jackson依赖
为了将对象序列化为XML文件,将XML反序列化为对象
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.8</version>
</dependency>
二、创建棋盘数据信息Bean对象
将稀疏数组存储为对象
public class SparseArray {
private int row;
private int col;
private int count;
private Array [] data;
public SparseArray() {}
public SparseArray(int row, int col, int count, Array[] data) {
this.row = row;
this.col = col;
this.count = count;
this.data = data;
}
public void setRow(int row) {
this.row = row;
}
public void setCol(int col) {
this.col = col;
}
public void setCount(int count) {
this.count = count;
}
public void setData(Array[] data) {
this.data = data;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public int getCount() {
return count;
}
public Array[] getData() {
return data;
}
}
public class Array {
private int row;
private int col;
private int value;
public Array() {}
public Array(int row, int col, int value) {
this.row = row;
this.col = col;
this.value = value;
}
public void setRow(int row) {
this.row = row;
}
public void setCol(int col) {
this.col = col;
}
public void setValue(int value) {
this.value = value;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public int getValue() {
return value;
}
}
三、实现棋盘存储与恢复
将数据对象序列化到XML配置文件中,实现存储棋盘
将XML配置反序列化为数据对象,实现恢复棋盘
public class SparsearrayDemo1 {
public static void main(String[] args) {
// 创建一个原始的二维数组
// 0:表示没有棋子,1:表示黑子,2:表示白子
int [][]chessArray = new int [11][11];
chessArray[1][2] = 1;
chessArray[2][3] = 2;
chessArray[4][5] = 2;
System.out.println("原数组内容:");
for (int[] ints : chessArray) {
System.out.println(Arrays.toString(ints));
}
// 遍历二维数组获得非零元素个数
int count = 0;
for (int[] value : chessArray) {
for (int j = 0; j < value.length; j++) {
if (value[j] != 0) {
count++;
}
}
}
System.out.println("非零个数:" + count);
int [][] sparseArray = new int [count+1][3];
int row = chessArray.length;
int col = chessArray[0].length;
sparseArray[0][0] = row;
sparseArray[0][1] = col;
sparseArray[0][2] = count;
int num = 0;
for (int i = 0; i < chessArray.length; i++) {
for (int j = 0; j < chessArray[i].length; j++) {
if (chessArray[i][j]!=0){
sparseArray[++num][0] = i;
sparseArray[num][1] = j;
sparseArray[num][2] = chessArray[i][j];
}
}
}
//创建存储棋盘数据的对象
SparseArray info = new SparseArray();
info.setRow(sparseArray[0][0]);
info.setCol(sparseArray[0][1]);
info.setCount(sparseArray.length-1);
Array [] arrays = new Array[sparseArray.length-1];
for (int i = 1; i < sparseArray.length; i++) {
Array array = new Array();
array.setRow(sparseArray[i][0]);
array.setCol(sparseArray[i][1]);
array.setValue(sparseArray[i][2]);
arrays[i-1] = array;
}
info.setData(arrays);
System.out.println("开始存储棋盘1");
File file = new File("./sparsearray.xml");
saveBoard(file,info);
System.out.println("棋盘存储成功");
System.out.println("开始恢复棋盘");
int[][] ints = recoverBoard(file, SparseArray.class);
for (int[] anInt : ints) {
System.out.println(Arrays.toString(anInt));
}
System.out.println("棋盘恢复成功");
}
public static void saveBoard(File file,SparseArray info){
//将对象序列化为xml文件存储
try {
if(!file.exists()){
file.createNewFile();
}
fromObjectToXML(file,info);
} catch (IOException e) {
e.printStackTrace();
}
}
public static int [][] recoverBoard(File file,Class cls){
if(file.exists()){
SparseArray info = (SparseArray)fromXMLToObject(file, cls);
int [][] newArray = new int [info.getRow()][info.getCol()];
Array[] data = info.getData();
for (int i = 0; i < data.length; i++) {
newArray[data[i].getRow()][data[i].getCol()] = data[i].getValue();
}
return newArray;
}
return null;
}
public static void fromObjectToXML(File file,Object data){
BufferedWriter bw = null;
XmlMapper xmlMapper = new XmlMapper();
try {
String xml = xmlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(data);
bw = new BufferedWriter(new FileWriter(file));
bw.write(xml,0,xml.length());
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Object fromXMLToObject(File file,Class cls){
BufferedReader br = null;
XmlMapper xmlMapper = new XmlMapper();
Object sparseArray = null;
try {
sparseArray = xmlMapper.readValue(new FileInputStream(file), cls);
} catch (IOException e) {
e.printStackTrace();
}
return sparseArray;
}
}
四、查看存储的棋局
<SparseArray>
<row>11</row>
<col>11</col>
<count>3</count>
<data>
<data>
<row>1</row>
<col>2</col>
<value>1</value>
</data>
<data>
<row>2</row>
<col>3</col>
<value>2</value>
</data>
<data>
<row>4</row>
<col>5</col>
<value>2</value>
</data>
</data>
</SparseArray>
五、控制台打印结果
原数组内容:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
非零个数:3
开始存储棋盘
棋盘存储成功
开始恢复棋盘
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
棋盘恢复成功
原文作者:絷缘
作者邮箱:zhiyuanworkemail@163.com
原文地址:https://zhiyuandnc.github.io/ZZGM9B12P/
版权声明:本文为博主原创文章,转载请注明原文链接作者信息