二维数组转稀疏数组,写入文件后再读取文件,将内容转回二维数组
生活随笔
收集整理的這篇文章主要介紹了
二维数组转稀疏数组,写入文件后再读取文件,将内容转回二维数组
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
該方法模擬的是將棋盤(pán)的位置保存到稀疏數(shù)組中,降低存儲(chǔ)的數(shù)據(jù)量,通過(guò)寫(xiě)入磁盤(pán)做持久化,再讀入后恢復(fù)棋盤(pán)內(nèi)容。
package com.moson.sparsearray;import java.io.*;/*** 數(shù)組轉(zhuǎn)稀疏數(shù)組再轉(zhuǎn)回?cái)?shù)組* @author moxingjian* @version 1.0* @date 10/10/19 7:40 PM*/ public class SparseArray {public static void main(String[] args) {// 初始化數(shù)組int[][] cheerArray = new int[11][11];// 添加值,添加的是棋盤(pán)的位置cheerArray[1][2] = 1;cheerArray[2][3] = 2;cheerArray[3][6] = 1;cheerArray[4][4] = 2;System.out.println("原始數(shù)組~");for (int[] row : cheerArray) {for (int data : row) {System.out.printf("%d\t" , data);}System.out.println();}// 獲取非0的個(gè)數(shù)int sum = 0;for (int i = 0; i < 11; i++) {for (int j = 0; j < 11; j++) {if (cheerArray[i][j] != 0) {sum++;}}}// 初始化稀疏數(shù)組int[][] sparseArray = new int[sum+1][3];// 第一行記錄的是:行、列數(shù)和非0的個(gè)數(shù)sparseArray[0][0] = 11;sparseArray[0][1] = 11;sparseArray[0][2] = sum;// 記錄非0的行數(shù)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("得到稀疏數(shù)組~");// 輸出稀疏數(shù)組/* 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 {//創(chuàng)建文件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("從文件中讀取稀疏數(shù)組~");// 從文件中讀取稀疏數(shù)組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);// 將每一行轉(zhuǎn)為數(shù)組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();}}}// 將稀疏數(shù)組轉(zhuǎn)回二維數(shù)組/* int[][] sourceCheerArray = new int[sparseArray[0][0]][sparseArray[0][1]];// 將稀疏數(shù)組中記錄的值填充回二維數(shù)組for (int i = 1; i < sparseArray.length; i++) {sourceCheerArray[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];}*/System.out.println("得到原始二維數(shù)組~");// 輸出源二維數(shù)組for (int[] row : sourceCheerArray) {for (int data : row) {System.out.printf("%d\t", data);}System.out.println();}} }總結(jié)
以上是生活随笔為你收集整理的二维数组转稀疏数组,写入文件后再读取文件,将内容转回二维数组的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 《R语言实战》第7章
- 下一篇: Java面试题:在一个递增的数组里面,找