700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 二维数组转稀疏数组 写入文件后再读取文件 将内容转回二维数组

二维数组转稀疏数组 写入文件后再读取文件 将内容转回二维数组

时间:2021-01-19 23:13:02

相关推荐

二维数组转稀疏数组 写入文件后再读取文件 将内容转回二维数组

该方法模拟的是将棋盘的位置保存到稀疏数组中,降低存储的数据量,通过写入磁盘做持久化,再读入后恢复棋盘内容。

package com.moson.sparsearray;import java.io.*;/*** 数组转稀疏数组再转回数组* @author moxingjian* @version 1.0* @date 10/10/19 7:40 PM*/public class SparseArray {public static void main(String[] args) {// 初始化数组int[][] cheerArray = new int[11][11];// 添加值,添加的是棋盘的位置cheerArray[1][2] = 1;cheerArray[2][3] = 2;cheerArray[3][6] = 1;cheerArray[4][4] = 2;System.out.println("原始数组~");for (int[] row : cheerArray) {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 (cheerArray[i][j] != 0) {sum++;}}}// 初始化稀疏数组int[][] sparseArray = new int[sum+1][3];// 第一行记录的是:行、列数和非0的个数sparseArray[0][0] = 11;sparseArray[0][1] = 11;sparseArray[0][2] = sum;// 记录非0的行数int count = 0;// 填充稀疏矩阵剩下的行for (int i = 0; i < 11; i++) {for (int j = 0; j < 11; j++) {if (cheerArray[i][j] != 0) {count++;sparseArray[count][0] = i;sparseArray[count][1] = j;sparseArray[count][2] = cheerArray[i][j];}}}System.out.println("得到稀疏数组~");// 输出稀疏数组/* for (int[] row : sparseArray) {for (int data : row) {System.out.printf("%d\t", data);}System.out.println();}*/for (int i = 0; i < sparseArray.length; i++) {System.out.printf("%d\t%d\t%d\t\n", sparseArray[i][0], sparseArray[i][1], sparseArray[i][2]);}String pathname = "/Users/moxingjian/IdeaProjects/DataStructures/src/main/java/com/moson/sparsearray/saprseArray.txt";// 输出到文件中File file = new File(pathname);FileOutputStream fileOutputStream = null;if (!file.exists()) {try {//创建文件file.createNewFile();} catch (IOException e) {e.printStackTrace();}}try {fileOutputStream = new FileOutputStream(file, true);for (int i = 0; i < sparseArray.length; i++) {StringBuffer stringBuffer = new StringBuffer();stringBuffer.append( sparseArray[i][0] + "\t" + sparseArray[i][1] + "\t" + sparseArray[i][2] + "\n");System.out.println(stringBuffer);fileOutputStream.write(stringBuffer.toString().getBytes("utf-8"));}} catch (FileNotFoundException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}System.out.println("从文件中读取稀疏数组~");// 从文件中读取稀疏数组File sparseArrayData = new File(pathname);int[][] sourceCheerArray = null;if (file.exists()) {FileReader fileReader = null;BufferedReader bufferedReader = null;try {fileReader = new FileReader(sparseArrayData);bufferedReader = new BufferedReader(fileReader);String line = null;int countSparse = 0;int number = 0;while ((line = bufferedReader.readLine()) != null) {System.out.println(line);// 将每一行转为数组String[] split = line.split("\t");Integer row = Integer.valueOf(split[0]);Integer col = Integer.valueOf(split[1]);Integer value = Integer.valueOf(split[2]);if (countSparse == 0) {sourceCheerArray = new int[row][col];} else {number++;sourceCheerArray[row][col] = value;}countSparse++;}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {bufferedReader.close();fileReader.close();} catch (IOException e) {e.printStackTrace();}}}// 将稀疏数组转回二维数组/* int[][] sourceCheerArray = new int[sparseArray[0][0]][sparseArray[0][1]];// 将稀疏数组中记录的值填充回二维数组for (int i = 1; i < sparseArray.length; i++) {sourceCheerArray[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];}*/System.out.println("得到原始二维数组~");// 输出源二维数组for (int[] row : sourceCheerArray) {for (int data : row) {System.out.printf("%d\t", data);}System.out.println();}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。