JAVA中稀疏数组与正常数组的转变

JAVA中稀疏数组与正常数组的转变

import java.io.*;
import java.util.ArrayList;
import java.util.List;

// 二维数组到稀疏数组的转变与恢复
public class demo1 {
    public static void main(String[] args) {
       // 创建原始的二维数组
       // 0 表示无效数据 其他表示有效数据
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[8][9] = 2;
        System.out.println("原始的二维数组");
        for(int[] row :chessArr1){
            for (int data :row){
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }
        int sum = 0;
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0;j<chessArr1[i].length;j++){
                if (chessArr1[i][j] != 0) {
                    sum ++;
                }
            }
        }
        // 创建稀疏数组
        int sparseArr[][] = new int[sum+1][3];
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;
        int startIndex = 1;
        System.out.println("二维数组转为稀疏数组");
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0;j<chessArr1[i].length;j++){
                if (chessArr1[i][j] != 0) {
                    sparseArr[startIndex][0] = i;
                    sparseArr[startIndex][1] = j;
                    sparseArr[startIndex][2] = chessArr1[i][j];
                    startIndex++;
                }
            }
        }
        // 存储稀疏数组
        File file = new File("sparseArr.data");
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            for(int[] row :sparseArr){
                for (int data :row){
                    bw.write(data+"\t");
                }
                bw.write("\n");
                bw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 从文件读取
        File src = new File("sparseArr.data");
        BufferedReader br = null;
        List<Integer> list = new ArrayList<Integer>();
        try {
            br = new BufferedReader(new FileReader(src));
            String line;
            while ((line = br.readLine())!= null) {
                String[] str = line.split("\t");
                for (int i = 0;i<str.length;i++) {
                    list.add(Integer.parseInt(str[i]));
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 恢复稀疏数组
        int j = 0;
        int sparseArr2[][] = new int[list.get(2)+1][3];
        for (int i = 0 ;i<list.size();i = i + 3){
            sparseArr2[j][0] = list.get(i);
            sparseArr2[j][1] = list.get(i+1);
            sparseArr2[j][2] = list.get(i+2);
            j++;
        }
        // 根据稀疏数组恢复原始数组
        int recoverArr[][] = new int[sparseArr2[0][0]][sparseArr2[0][1]];
        for (int i = 1;i<=sparseArr2[0][2];i++){
            recoverArr[sparseArr2[i][0]][sparseArr2[i][1]] = sparseArr2[i][2];
        }
        // 恢复后的结果
        System.out.println("恢复后的二维数组");
        for(int[] row :recoverArr){
            for (int data :row){
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我爱RMB

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

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

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

打赏作者

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

抵扣说明:

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

余额充值