稀疏数组---二维数组的转化与读写文件

什么叫稀疏数组呢?
如果一个数组(包括多维数组)中的大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组,节约空间。
一般来说,稀疏数组的处理方法是:
1.记录数组一共有几行几列,有多少个不同的数值。
2.把具有不同值的元素的行列及记录在一个小规模的数组中,从而缩小程序的规模。

package datastructures;

import java.io.*;

/**
 *二维数组转稀疏数组的思路:
 * 1.遍历原始的二维数组,得到有效数据的个数sum
 * 2.根据sum就可以创建稀疏数组sparseArr  int[sum+1][3]
 * 3.将二维数组的有效数据存入到稀疏数组
 *
 * 稀疏数组转原始二维数组的思路
 * 1.先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
 * 2.在读取稀疏数组的后几行的数据,并赋给原始的二维数组即可
 */
public class SparseArray {

    public static void main(String[] args) throws IOException {
        //创建11*11的棋盘二维数组,并给部分位置赋值
        int chessArray[][] = new int[11][11];
        int sum = 0;
        chessArray[1][2] = 1;
        chessArray[2][3] = 2;
//        遍历chessArray
        for (int i = 0; i < chessArray.length; i++) {
            for (int j = 0; j < chessArray[i].length; j++) {
                System.out.print("\t" + chessArray[i][j]);
                if (chessArray[i][j] != 0) {
                    sum++;
                }
            }
            System.out.println();
        }

        System.out.println("sum=" + sum);

        //根据二维数组创建稀疏数组
        int sparseArray[][] = new int[sum + 1][3];
        sparseArray[0][0] = 11;
        sparseArray[0][1] = 11;
        sparseArray[0][2] = sum;
        //给稀疏数组赋值
        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++;
                    sparseArray[count][0] = i;
                    sparseArray[count][1] = j;
                    sparseArray[count][2] = chessArray[i][j];
                }
            }
        }

        //将稀疏数组写入文件

        FileWriter out = new FileWriter("sparseArray.txt");  //文件写入流

        //将数组中的数据写入到文件中。每行各数据之间TAB间隔
        for (int i = 0; i < sparseArray.length; i++) {
            for (int j = 0; j < sparseArray[i].length; j++) {
                out.write(sparseArray[i][j] + "\t");
            }
            out.write("\n");
        }
        out.close();


       //读取文件中的稀疏数组
        int row = 0;
        BufferedReader br = new BufferedReader(new FileReader("sparseArray.txt"));
        String line;
        while ((line = br.readLine()) != null) {
            String temp[] = line.split("\t");
            for (int j = 0; j < temp.length; j++) {
                sparseArray[row][j] = Integer.parseInt(temp[j]);
            }
            row++;
        }

	    br.close();

    //显示读取出的数组
	  System.out.println("显示读出来的数组");
	  for(int i=0;i<sparseArray.length;i++)
    {
        for (int j = 0; j < 3; j++) {
            System.out.print(sparseArray[i][j] + "\t");
        }
        System.out.println();
        }
     //还原棋盘,为棋盘赋值
     int chessArray2[][] = new int[sparseArray[0][0]][sparseArray[0][1]];
	  for (int i = 1 ; i<sparseArray.length ; i++) {
          chessArray2[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
      }
        System.out.println("恢复后的二维数组");

        for (int[] row1 : chessArray2) {
            for (int data : row1) {
                System.out.print("\t"+ data);
            }
            System.out.println();
        }

    }
    }
发布了43 篇原创文章 · 获赞 2 · 访问量 989

猜你喜欢

转载自blog.csdn.net/study_azhuo/article/details/105577949