这两天开始学习数据结构,这是存储棋盘的作业,因为网上其它方法都有了,为了复习IO流,就使用序列化的方式做了一下练习。
import java.io.*;
import java.util.ArrayList;
public class SparseArray {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建一个原始的二维数组 11*11
//0:表示没有棋子 1 表示黑子 2表示篮子
int chessArr1[][] = new int[11][11];
//有效位置数据
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
chessArr1[3][4] = 2;
chessArr1[3][5] = 1;
//输出原始的二维数组
/*for (int row[] : chessArr1) {
for (int data: row){
System.out.printf("%d\t",data);
}
System.out.println();
}*/
//将原始的二维数组输出为稀疏数组
//1.遍历原数组,得到有效个数
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++