数组与稀疏数组之间的转换算法实现 java

数组与稀疏数组之间的转换算法实现 java

主要方法

方法主要包括:

  • 二维数组转换到稀疏数组
  • 稀疏数组转换到二维数组
  • 将数组写入到文件中
  • 将文件中的数据读取出来

代码`

package com.atguigu.sparsearray;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 *
 * 稀疏数组的代码实现
 *
 *
 * 五子棋的模拟
 * 黑子用1表示,蓝子使用2表示
 * 11*11
 *
 *
 * 建立一个二维数组
 * 在第二行,第三列输入1
 * 在第三行,第四列输入2
 * 在第四行,第五列输入2
 *
 *
 * 方法主要包括:
 * 二维数组转换到稀疏数组
 * 稀疏数组转换到二维数组
 * 将数组写入到文件中
 * 将文件中的数据读取出来
 */
public class SparesArray {
    public static void main(String[] args) {

        //声明参数
        int sum = 0;
        int count = 0;

        //建立二维数组
        int[][] chessArray = new int[11][11];
        chessArray[1][2] = 1;
        chessArray[2][3] = 2;
        chessArray[3][4] = 2;

        //遍历二维数组
        System.out.println("原始的二位数组");
        lookChessArray(chessArray);

        //遍历二维数组,得到非零的个数
        sum = getNum(chessArray);
        System.out.println("有效数据个数为" + sum);

        //建立稀疏数组
        int[][] spares = new int[sum + 1][3];
        spares[0][0] = chessArray.length;
        spares[0][1] = chessArray.length;
        spares[0][2] = sum;

        //将二维数组转换为稀疏数组
        changeArray(chessArray,spares);

        //稀疏数组的遍历
        lookspares(spares);


        //建立新的二维数组
        int[][] chessArray2 = new int[spares[0][0]][spares[0][1]];
        //将稀疏数组转换为二维数组
        changeArray2(spares,chessArray2);
        //遍历二维数组
        System.out.println("转化来的二维数组为");
        lookChessArray(chessArray2);

        //写入文件中
        inputFlies(spares);

        //从文件中读取出来
          int[][] spa = readFiles();
        System.out.println("从文件中写入的稀疏数组遍历结果为");
          lookChessArray(spa);


    }

    private static int[][] readFiles() {
        FileInputStream fis = null;
        int[][] spares = new int[0][];
        try {
            fis = new FileInputStream("D:/input.txt");
//            System.out.println(fis.available());
            int count = fis.available();
//            System.out.println(count);
             spares = new int[count / 3][3];
            for (int i = 0; i < spares.length; i++ ) {
                spares[i][0] = fis.read();
                spares[i][1] = fis.read();
                spares[i][2] = fis.read();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fis != null) {
                try {
                    fis.close();
                    System.out.println("输出成功");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return spares;
    }

    private static void inputFlies(int[][] spares) {

        //将二维数组转换为一维数组array
        byte[] array = new byte[(spares.length)*(spares[0].length)];
        int j = 0;
        for (int i = 0; i < spares.length ; i++) {
                array[j] = (byte) spares[i][0];
                array[j + 1] = (byte) spares[i][1];
                array[j + 2] = (byte) spares[i][2];
                j = j + 3;
        }

        //文件写入
        input(spares,array);
     /*  //测试int[]
        System.out.println("--------------");
        for (int i = 0; i < array.length ; i++) {
            System.out.print("\t" + array[i]);
        }*/

    }

    private static void input(int[][] spares,byte[] array) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("D:/input.txt");
            fos.write(array);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                    System.out.println("成功写入硬盘");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        for (int i = 0; i < spares.length; i++) {

        }
    }

    private static void changeArray2(int[][] spares, int[][] chessArray2) {
        for (int i = 1; i < spares.length; i++) {
            chessArray2[spares[i][0]][spares[i][1]] = spares[i][2];
        }
    }

    private static void lookspares(int[][] spares) {
        System.out.println("稀疏数组如下所示");
        for (int i = 0; i < spares.length ; i++) {
            for (int j = 0; j < spares[i].length ; j++) {
                System.out.printf("%d\t",spares[i][j]);
            }
            System.out.println("");
        }
    }

    private static void changeArray(int[][] chessArray, int[][] spares) {
        int count = 0;
        for (int i = 0; i < chessArray.length; i++) {
            for (int j = 0; j < chessArray[i].length; j++) {
                if (chessArray[i][j] != 0) {
                    count++;
                    spares[count][0] = i;
                    spares[count][1] = j;
                    spares[count][2] = chessArray[i][j];

                }
            }
        }
    }

    private static int getNum(int[][] chessArray) {
        int sum = 0;
        for (int i = 0; i < chessArray.length; i++) {
            for (int j = 0; j < chessArray[i].length; j++) {
                if (chessArray[i][j] != 0) {
                    sum++;
                }
            }
        }
      return sum;
    }

    private static void lookChessArray(int[][] chessArray) {
        for (int[] row :chessArray) {
            for (int data:row) {
                System.out.printf("%3d\t",data);
            }
            System.out.println();
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值