zoukankan      html  css  js  c++  java
  • Java实现二维数组和稀疏数组的相互转换

    package datastructure.sparsearray;

    import javax.lang.model.element.VariableElement;
    import java.io.*;
    import java.util.ArrayList;

    /**
    * 二维数组<--->稀疏数组
    * 存盘/读盘
    *
    * @author clownadam
    */
    public class SparseArray {
    public static void main(String[] args) throws IOException {
    /*创建一个原始的二维数组11*11*/
    int[][] chessArr1 = new int[11][11];
    chessArr1[1][2] = 1;
    chessArr1[2][3] = 2;
    chessArr1[4][5] = 2;
    /*输出原始的二维数组*/
    System.out.println("原始数组--------");
    for (int[] rows : chessArr1) {
    for (int data : rows) {
    System.out.printf("%d", data);
    }
    System.out.println();
    }
    /*将二维数组转成稀疏数组
    * 1.遍历二维数组,得到有效数据(非0数据个数)
    * */
    int sum = 0;
    for (int row = 0; row < chessArr1.length; row++) {
    for (int col = 0; col < chessArr1.length; col++) {
    if (0 != chessArr1[row][col]) {
    sum++;
    }
    }
    }
    /*将二维数组转成稀疏数组
    * 2.创建对应的稀疏数组
    * */
    int[][] sparseArr = new int[sum + 1][3];
    //给稀疏数组赋值
    sparseArr[0][0] = chessArr1.length;
    sparseArr[0][1] = chessArr1.length;
    sparseArr[0][2] = sum;
    /*用于记录是第几个非0数据*/
    int count = 0;
    //二维数组的非0数据存入稀疏数组
    for (int row = 0; row < chessArr1.length; row++) {
    for (int col = 0; col < chessArr1.length; col++) {
    if (0 != chessArr1[row][col]) {
    /*chessArr1[row][col]*/
    count++;
    sparseArr[count][0] = row;
    sparseArr[count][1] = col;
    sparseArr[count][2] = chessArr1[row][col];
    }
    }
    }
    /*输出稀疏数组的*/
    System.out.println();
    System.out.println("稀疏数组--------");
    for (int[] rows : sparseArr) {
    for (int row : rows) {
    System.out.printf("%d ", row);
    }
    System.out.println();
    }
    /*将稀疏数组写入文件*/
    // writeSparseArrToFile(sparseArr);
    // readSparseArrToFile("sparse.txt");
    /*将稀疏数组恢复成原始的二维数组
    * 1.先读取稀疏数组第一行,根据第一行的数据,创建原始的二维数组
    * */
    int row = sparseArr[0][0];
    int col = sparseArr[0][1];
    int[][] chessArr2 = new int[row][col];
    for (int i = 1; i < sparseArr.length; i++) {
    chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
    }
    /*输出二维数组*/
    System.out.println("二维数组--------");
    for (int[] rows : chessArr2) {
    for (int data : rows) {
    System.out.printf("%d", data);
    }
    System.out.println();
    }
    /*-------------------读取稀疏数组文件case-----------------------------*/
    int[][] demos = readSparseArrToFile("sparse.txt");
    for (int[] demo : demos) {
    for (int i : demo) {
    System.out.print(i+" ");
    }
    System.out.println();
    }

    }

    /**
    * 读取文本文件并将其转换为稀疏数组
    * @param fileName 文件名
    * @return 稀疏数组
    * @throws IOException
    */
    private static int[][] readSparseArrToFile(String fileName) throws IOException {
    File sparseFile = new File(fileName);
    FileReader reader = new FileReader(sparseFile);
    BufferedReader br = new BufferedReader(reader);
    String line;
    ArrayList<int[]> list = new ArrayList<>();
    int rows = 0;
    while ((line = br.readLine()) != null) {
    String[] str = line.split(" ");
    if(rows<str.length){
    rows = str.length;
    }
    int[] col = new int[3];
    for (int i = 0; i < str.length; i++) {
    col[i] = Integer.parseInt(str[i]);
    System.out.println(col[i]);
    }
    list.add(col);
    }
    int[][] sparseArr = new int[list.size()][rows];
    for (int i = 0; i < list.size(); i++) {
    for (int j = 0; j < rows; j++) {
    sparseArr[i][j] = list.get(i)[j];
    }
    }
    br.close();
    reader.close();
    return sparseArr;

    }

    /**
    * 实现将稀疏数组写到磁盘文件中
    * @param sparseArr 稀疏数组
    * @throws IOException
    */
    private static void writeSparseArrToFile(int[][] sparseArr) throws IOException {
    File sparseFile = new File("sparse.txt");
    FileWriter writer = new FileWriter(sparseFile);
    BufferedWriter bw = new BufferedWriter(writer);
    for (int[] row : sparseArr) {
    for (int data : row) {
    bw.write(data+" ");
    }
    bw.write(" ");
    bw.flush();
    }
    bw.close();
    writer.close();
    }
    }
  • 相关阅读:
    Tarjan算法求双连通分量
    Tarjan
    前端技术实现js图片水印
    记录一下ionic canvas图片,还有canvas里面的图片跨域的问题
    ionic cordova screenshot 使用和操作
    关于ionic2 更新到ionic3 后组件不能用的解决方案
    背景图处理,这是个好东西记录一下
    radio样式的写法,单选和多选如何快速的改变默认样式,纯CSS,
    ionic使用cordova插件中的Screenshot截图分享功能
    ionic中执行pop返回上一个页面,还需要执行操作
  • 原文地址:https://www.cnblogs.com/clownAdam/p/12901101.html
Copyright © 2011-2022 走看看