在一个使用二维数组保存围棋棋盘和还原围棋棋盘的过程来加深对稀疏矩阵的理解
如右图所示,将棋盘形象的表示为右边的二维数组,1表示黑子,2表示篮子
我们可以用右边的二维数组来保存这个数组,但是我们还可以用稀疏数组来表示这个二维数组,
它的好处就是减小了保存棋盘的数组大小,减小了棋盘复原的遍历时间
原理很简单,接下来直接上代码:
1.第一步:用二维数组将棋盘表示出来
package sparsearray;
import java.io.*;
/**
* @filename SparseArray
* @description 稀疏矩阵的实现
* @action 压缩和恢复五子棋棋盘
* @author market
* @date 2021/7/6 9:08
*/
public class SparseArray {
public static void main(String[] args) {
//定义一个二维数组,1表示下黑子,2表示下蓝子,0表示空白
int[][] chessArr = new int[11][11];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
System.out.println("初始二维数组的内容如下~~~~");
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
System.out.print(chessArr[i][j] + "\t");
}
System.out.println();
}
}
}
输出结果如下:
2.第二步:将二维数组转换为稀疏数组
package sparsearray;
import java.io.*;
/**
* @filename SparseArray
* @description 稀疏矩阵的实现
* @action 压缩和恢复五子棋棋盘
* @author market
* @date 2021/7/6 9:08
*/
public class SparseArray {
public static void main(String[] args) {
//定义一个二维数组,1表示下黑子,2表示下蓝子,0表示空白
int[][] chessArr = new int[11][11];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
System.out.println("初始二维数组的内容如下~~~~");
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
System.out.print(chessArr[i][j] + "\t");
}
System.out.println();
}
//获取元素二维数组中有多少个数据
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr[i][j]!=0){
sum++;
}
}
}
//给稀疏数组赋值
int sparseArr[][] = new int[sum+1][3];
sparseArr[0][0] = chessArr.length;
sparseArr[0][1] = chessArr[0].length;
sparseArr[0][2] = sum;
int count=0;
for (int i = 0; i < chessArr.length; i++) {
for (int j = 0; j < chessArr[0].length; j++) {
if (chessArr[i][j]!=0){
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr[i][j];
}
}
}
System.out.println("二维数组转为稀疏数组后的值~~~~~");
for (int i = 0; i < sparseArr.length; i++) {
System.out.print(sparseArr[i][0]+ "\t" +sparseArr[i][1]+ "\t" +sparseArr[i][2]+"\n");
}
}
运行结果如下
3.第三步:将压缩后的稀疏数组存入map.data文件中(存档)
package sparsearray;
import java.io.*;
/**
* @filename SparseArray
* @description 稀疏矩阵的实现
* @action 压缩和恢复五子棋棋盘
* @author market
* @date 2021/7/6 9:08
*/
public class SparseArray {
public static void main(String[] args) {
//定义一个二维数组,1表示下黑子,2表示下蓝子,0表示空白
int[][] chessArr = new int[11][11];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
System.out.println("初始二维数组的内容如下~~~~");
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
System.out.print(chessArr[i][j] + "\t");
}
System.out.println();
}
//获取元素二维数组中有多少个数据
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr[i][j]!=0){
sum++;
}
}
}
//给稀疏数组赋值
int sparseArr[][] = new int[sum+1][3];
sparseArr[0][0] = chessArr.length;
sparseArr[0][1] = chessArr[0].length;
sparseArr[0][2] = sum;
int count=0;
for (int i = 0; i < chessArr.length; i++) {
for (int j = 0; j < chessArr[0].length; j++) {
if (chessArr[i][j]!=0){
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr[i][j];
}
}
}
System.out.println("二维数组转为稀疏数组后的值~~~~~");
for (int i = 0; i < sparseArr.length; i++) {
System.out.print(sparseArr[i][0]+ "\t" +sparseArr[i][1]+ "\t" +sparseArr[i][2]+"\n");
}
//将二维数组存入map.data文件中
try {
FileOutputStream outputStream = new FileOutputStream("map.data");
for (int i = 0; i < sparseArr.length; i++) {
outputStream.write((sparseArr[i][0]+"\t").getBytes());
outputStream.write((sparseArr[i][1]+"\t").getBytes());
outputStream.write((sparseArr[i][2]+"\n").getBytes());
}
} catch (FileNotFoundException e) {
System.out.println("文件异常~~~~");
e.printStackTrace();
}catch (IOException e){
System.out.println("写入异常~~~~");
e.printStackTrace();
}
}
}
保存结果map.data文件中
4.第四步:读取文件(读盘)
package sparsearray;
import java.io.*;
/**
* @filename SparseArray
* @description 稀疏矩阵的实现
* @action 压缩和恢复五子棋棋盘
* @author market
* @date 2021/7/6 9:08
*/
public class SparseArray {
public static void main(String[] args) {
//定义一个二维数组,1表示下黑子,2表示下蓝子,0表示空白
int[][] chessArr = new int[11][11];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
System.out.println("初始二维数组的内容如下~~~~");
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
System.out.print(chessArr[i][j] + "\t");
}
System.out.println();
}
//获取元素二维数组中有多少个数据
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr[i][j]!=0){
sum++;
}
}
}
//给稀疏数组赋值
int sparseArr[][] = new int[sum+1][3];
sparseArr[0][0] = chessArr.length;
sparseArr[0][1] = chessArr[0].length;
sparseArr[0][2] = sum;
int count=0;
for (int i = 0; i < chessArr.length; i++) {
for (int j = 0; j < chessArr[0].length; j++) {
if (chessArr[i][j]!=0){
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr[i][j];
}
}
}
System.out.println("二维数组转为稀疏数组后的值~~~~~");
for (int i = 0; i < sparseArr.length; i++) {
System.out.print(sparseArr[i][0]+ "\t" +sparseArr[i][1]+ "\t" +sparseArr[i][2]+"\n");
}
//将二维数组存入map.data文件中
try {
FileOutputStream outputStream = new FileOutputStream("map.data");
for (int i = 0; i < sparseArr.length; i++) {
outputStream.write((sparseArr[i][0]+"\t").getBytes());
outputStream.write((sparseArr[i][1]+"\t").getBytes());
outputStream.write((sparseArr[i][2]+"\n").getBytes());
}
} catch (FileNotFoundException e) {
System.out.println("文件异常~~~~");
e.printStackTrace();
}catch (IOException e){
System.out.println("写入异常~~~~");
e.printStackTrace();
}
//还原数组,读取map.data文件
File file = new File("map.data");
byte b[] = new byte[(int)file.length()];
try {
FileInputStream inputStream = new FileInputStream(file);
inputStream.read(b);
inputStream.close();
} catch (FileNotFoundException e) {
System.out.println("文件异常~~~~");
e.printStackTrace();
}catch (IOException e){
System.out.println("写入异常~~~~");
e.printStackTrace();
}
String data = new String(b);
String[] rowStr = data.split("\n");
String[] colStrLen = rowStr[0].split("\t");
int[][] sparseArr2 = new int[rowStr.length][colStrLen.length];
for (int i = 0; i < rowStr.length; i++) {
String[] colStr = rowStr[i].split("\t");
for (int j = 0; j < colStr.length; j++) {
Integer integer = Integer.valueOf(colStr[j]);
sparseArr2[i][j] = integer;
}
}
System.out.println("从map.data文件中读取出来的数据转为稀疏数组后的值~~~~");
for (int i = 0; i < sparseArr2.length; i++) {
System.out.print(sparseArr2[i][0]+ "\t" +sparseArr2[i][1]+ "\t" +sparseArr2[i][2]+"\n");
}
}
}
运行结果如下
5.第五步:将稀疏数组还原为二维数组
package sparsearray;
import java.io.*;
/**
* @filename SparseArray
* @description 稀疏矩阵的实现
* @action 压缩和恢复五子棋棋盘
* @author market
* @date 2021/7/6 9:08
*/
public class SparseArray {
public static void main(String[] args) {
//定义一个二维数组,1表示下黑子,2表示下蓝子,0表示空白
int[][] chessArr = new int[11][11];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
System.out.println("初始二维数组的内容如下~~~~");
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
System.out.print(chessArr[i][j] + "\t");
}
System.out.println();
}
//获取元素二维数组中有多少个数据
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr[i][j]!=0){
sum++;
}
}
}
//给稀疏数组赋值
int sparseArr[][] = new int[sum+1][3];
sparseArr[0][0] = chessArr.length;
sparseArr[0][1] = chessArr[0].length;
sparseArr[0][2] = sum;
int count=0;
for (int i = 0; i < chessArr.length; i++) {
for (int j = 0; j < chessArr[0].length; j++) {
if (chessArr[i][j]!=0){
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr[i][j];
}
}
}
System.out.println("二维数组转为稀疏数组后的值~~~~~");
for (int i = 0; i < sparseArr.length; i++) {
System.out.print(sparseArr[i][0]+ "\t" +sparseArr[i][1]+ "\t" +sparseArr[i][2]+"\n");
}
//将二维数组存入map.data文件中
try {
FileOutputStream outputStream = new FileOutputStream("map.data");
for (int i = 0; i < sparseArr.length; i++) {
outputStream.write((sparseArr[i][0]+"\t").getBytes());
outputStream.write((sparseArr[i][1]+"\t").getBytes());
outputStream.write((sparseArr[i][2]+"\n").getBytes());
}
} catch (FileNotFoundException e) {
System.out.println("文件异常~~~~");
e.printStackTrace();
}catch (IOException e){
System.out.println("写入异常~~~~");
e.printStackTrace();
}
//还原数组,读取map.data文件
File file = new File("map.data");
byte b[] = new byte[(int)file.length()];
try {
FileInputStream inputStream = new FileInputStream(file);
inputStream.read(b);
inputStream.close();
} catch (FileNotFoundException e) {
System.out.println("文件异常~~~~");
e.printStackTrace();
}catch (IOException e){
System.out.println("写入异常~~~~");
e.printStackTrace();
}
String data = new String(b);
String[] rowStr = data.split("\n");
String[] colStrLen = rowStr[0].split("\t");
int[][] sparseArr2 = new int[rowStr.length][colStrLen.length];
for (int i = 0; i < rowStr.length; i++) {
String[] colStr = rowStr[i].split("\t");
for (int j = 0; j < colStr.length; j++) {
Integer integer = Integer.valueOf(colStr[j]);
sparseArr2[i][j] = integer;
}
}
System.out.println("从map.data文件中读取出来的数据转为稀疏数组后的值~~~~");
for (int i = 0; i < sparseArr2.length; i++) {
System.out.print(sparseArr2[i][0]+ "\t" +sparseArr2[i][1]+ "\t" +sparseArr2[i][2]+"\n");
}
//将稀疏数组还原为二维数组
int[][] chessArr2 = new int[sparseArr2[0][0]][sparseArr2[0][1]];
for (int i = 1; i < sparseArr2.length; i++) {
chessArr2[sparseArr2[i][0]][sparseArr2[i][1]] = sparseArr2[i][2];
}
System.out.println("将稀疏数组还原为二维数组后的值~~~~");
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
System.out.print(chessArr2[i][j] + "\t");
}
System.out.println();
}
}
}
运行结果如下: