clownAdam

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
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\t", 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+"\t");
}
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("\t");
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+"\t");
}
bw.write("\n");
bw.flush();
}
bw.close();
writer.close();
}
}
posted on 2020-05-16 16:38  clownAdam  阅读(248)  评论(0编辑  收藏  举报