稀疏数组学习

该博客介绍了如何将二维棋盘转换成稀疏数组,然后将稀疏数组写入文件,再从文件读取恢复成二维棋盘的过程。通过Java代码展示了具体的实现方法。
摘要由CSDN通过智能技术生成

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class SparseArray {
    
    //将二维棋盘转换成稀疏数组
    private  static void changeSparseArr(int chessArr[][],int sparseArr[][]){
                
                //从第二行开始循环遍历二维棋盘,将不为0的值的位置存到稀疏数组中
                int count=0;
                for(int i=1;i<11;i++){
                    for(int j=0;j<11;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.printf("%d\t%d\t%d\n", sparseArr[i][0],sparseArr[i][1],sparseArr[i][2]);
                }        
    }
    private static void writeFile(int sparseArr[][],int sum,File file){
                //创建文件写入流
                FileWriter out=null;
                try {
                     out=new FileWriter(file);
                    for(int i=0;i<sum+1;i++){
                        for(int j=0;j<3;j++){
                            out.write(sparseArr[i][j]+"\t");//将稀疏数组写入文件
                        }
                        out.write("\r\n");
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    try {
                        out.close();//关闭文件写入流
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
    }
    private static void readFile(File file,int sparseArr1[][]){
        //创建一个文件读入流
        FileReader read=null;
        //将从文件读取内容缓存到读缓存中
        BufferedReader in=null;
        try {
             read=new FileReader(file);
             in=new BufferedReader(read);
             String line;
             int row=0;
             try {
                while((line=in.readLine())!=null){
                     String []temp=line.split("\t");
                     for(int j=0;j<temp.length;j++){
                         sparseArr1[row][j]=Integer.parseInt(temp[j]);
                     }
                     row++;
                 }
                for(int i=0;i<sparseArr1.length;i++){
                    System.out.printf("%d\t%d\t%d\n", sparseArr1[i][0],sparseArr1[i][1],sparseArr1[i][2]);
                }
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                //关闭原则:先开后关
                in.close();
                read.close();
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    }
    
    private static void changeChessArr(int sparseArr1[][]){
                //将稀疏数组恢复成二维棋盘
                //创建二维棋盘大小,并从稀疏数组取值进行赋值
                int chessArr1[][]=new int[sparseArr1[0][0]][sparseArr1[0][1]];
                for(int i=1;i<sparseArr1.length;i++){
                    chessArr1[sparseArr1[i][0]][sparseArr1[i][1]]=sparseArr1[i][2];
                }
                //打印恢复棋盘
                System.out.println("恢复后的二维棋盘如下:");
                for(int[]row:chessArr1){
                    for(int data:row){
                        System.out.printf("%d\t", data);
                    }
                    System.out.println();
                }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //创建二维棋盘 大小11*11
        //0:表示没有棋子  1:表示黑子   2:表示篮子  
        //给棋盘赋初始位置值
        int chessArr[][] =new int[11][11];
        chessArr[1][2]=1;
        chessArr[2][3]=2;
        chessArr[4][5]=2;
        //打印二维棋盘
        System.out.println("初始二维棋盘为:");
        for(int[] row:chessArr){
            for(int data:row){
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
        
        //获取不为0数据的个数
        int sum=0;
        for(int i=0;i<11;i++){
            for(int j=0;j<11;j++){
                if(chessArr[i][j]!=0){
                    sum++;
                }
            }
        }
        System.out.println("sum="+sum);
        //创建稀疏数组大小
        int sparseArr[][]=new int[sum+1][3];
        //给第一行赋值
        sparseArr[0][0]=11;
        sparseArr[0][1]=11;
        sparseArr[0][2]=sum;
        //二维棋盘转换成稀疏数组
        changeSparseArr(chessArr,sparseArr);
        //创建文件路径
        File file=new File("D:\\数据结构和算法\\test2022326.txt");
        
        //将稀疏数组存到文件中
        writeFile(sparseArr,sum,file);
        
        //从文件读取内容恢复成稀疏数组
        System.out.println("从文件读取内容:");
        //创建数组
        int sparseArr1[][]=new int[sum+1][3];
        //从文件读取内容
        readFile(file,sparseArr1);
        //将稀疏数组转换成二维数组
        changeChessArr(sparseArr1);
        
    }

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

现在开始也不晚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值