稀疏数组

需求】

编写五子棋程序,使其实现退出和续上盘的功能

【分析】

续上盘功能可以通过IO技术,将得到的数组存储到文件中,复盘时,再从文件中读取

关于稀疏数组

【思路梳理】

【代码实现】

package suanfa;

import javax.print.DocFlavor;
import java.io.*;

public class SparseArray {

    public static void main(String[] args) throws IOException{
        //创建一个原始的二维数组
        //0:表示没有棋子,1:表示黑子,2:表示蓝子
        int chessAeel[][] = new int[11][11];
        
        chessAeel[1][2] = 1;
        chessAeel[2][3] = 2;
        chessAeel[4][5] = 2;
        
        //输出原始的二维数组
        System.out.println("原始的二维数组");
        for (int[] row:chessAeel) {
            for (int data: row) {
                
                System.out.printf("%d\t",data);
                
            }
            System.out.println();
        }
        
        /*
         *一、将二维数组 转 稀疏数组
         */
        //1、先遍历二维数组 得到非0 数据的个数
        int sum = 0;
        for (int i = 0; i < 11 ; i++) {
            for (int j = 0; j < 11 ; j++) {
                if (chessAeel[i][j]!=0)
                    sum++;
            }
        }
        
        //2、创建对应的稀疏数组
        int sparseArr[][] = new int[sum+1][3];
        //给稀疏数组赋值
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;
        
        //遍历二维数组,将非零的值存到spqrseArr中
        int count = 0; //count 用于记录是第几个非零数据
        for (int i = 0; i < 11 ; i++) {
            for (int j = 0; j < 11 ; j++) {
                if (chessAeel[i][j]!=0) {
                    
                    count++;
                    
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessAeel[i][j];
                    
                }
            }
        }
        
        //输出稀疏数组形式
        System.out.println();
        System.out.println("得到的稀疏数组为~~~");
        for (int i = 0; i < sparseArr.length ; i++) {
            System.out.printf("%d\t%d\t%d\t\n",sparseArr[i][0],sparseArr[i][1],sparseArr[i][2]);
        }
        
        //将稀疏数组存储到文件map.data中
        saveData(sparseArr);


        //从map.data文件中读取稀疏数组
        int[][] ch2 =getData();
        
        /*
         *二、将稀疏数组转换为二维数组
         */
        //1、先读取稀疏数组的第一行,根据第一行的数据,创建原始二维数组
        
        int chessArr2[][] = new int[ch2[0][0]][ch2[0][1]];
        
        //2、再读取稀疏数组后继行的数据,从第二行开始,并付给原始的二维数组
        for (int i=1;i<ch2[0][2]+1;i++){
            chessArr2[ch2[i][0]][ch2[i][1]] = ch2[i][2];
        }
        
        //输出恢复后的二维数组
        System.out.println();
        System.out.println("恢复后的二维数组");
        
        for (int[] row : chessArr2){
            for (int data : row) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
            
        }
        
    }
    
    public static void  saveData(int[][] arr) throws IOException{
        
        File file = new File("D:\\IntelliJWorkSpace\\src\\suanfa\\map.data");
        PrintWriter pw = new PrintWriter(new FileWriter(file));
        for (int i=0;i < arr.length; i++){
            pw.println(arr[i][0]+","+arr[i][1]+","+arr[i][2]);
            pw.flush();
        }
    }
    
    public static int[][] getData()throws IOException{

        File file = new File("D:\\IntelliJWorkSpace\\src\\suanfa\\map.data");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String len=br.readLine();
        String[] str = len.split(",");
        int[][] ch = new  int[Integer.parseInt(str[2])+1][3];
        ch[0][0] = Integer.parseInt(str[0]);
        ch[0][1] = Integer.parseInt(str[1]);
        ch[0][2] = Integer.parseInt(str[2]);
        int temp = 1;
        while((len=br.readLine())!=null){
            for (int i = 0 ;i < 3;i++){
                String[] str2 = len.split(",");
                ch[temp][i] = Integer.parseInt(str2[i]);
            }
            temp++;
        }
        return ch;
    }
    
}

【效果展示】

文件截图

运行结果

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值