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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/51546.html
標籤:其他
上一篇:樹和二叉樹
下一篇:雙wan路由器上內外網
