二维数组转稀疏数组,写入文件后再读取文件,将内容转回二维数组
Posted starjuly
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二维数组转稀疏数组,写入文件后再读取文件,将内容转回二维数组相关的知识,希望对你有一定的参考价值。
该方法模拟的是将棋盘的位置保存到稀疏数组中,降低存储的数据量,通过写入磁盘做持久化,再读入后恢复棋盘内容。
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();
以上是关于二维数组转稀疏数组,写入文件后再读取文件,将内容转回二维数组的主要内容,如果未能解决你的问题,请参考以下文章