淺談Java數(shù)據(jù)結(jié)構(gòu)之稀疏數(shù)組知識(shí)總結(jié)
稀疏數(shù)組
- 當(dāng)一個(gè)數(shù)組中的元素大多為0或者相同元素的時(shí)候,可以用稀疏數(shù)組來壓縮
- 稀疏數(shù)組只記錄 行row 列col 值value
將下列的二維數(shù)組轉(zhuǎn)為稀疏數(shù)組,如下兩圖所示


1.實(shí)現(xiàn)二維數(shù)組轉(zhuǎn)為稀疏數(shù)組的步驟:
- 遍歷數(shù)組,得到數(shù)組中 不為0的個(gè)數(shù),并記錄為sum,作為稀疏數(shù)組第0行的 value
- 遍歷數(shù)組,將數(shù)組中不為0的數(shù)的行和列和值分別寫入稀疏數(shù)組的 row col val 中
代碼實(shí)現(xiàn):
public class SparseArray {
public static void main(String[] args) {
// 創(chuàng)建一個(gè)原始二維數(shù)組
// 0表示沒有棋子,1,2各表示一種棋
int chessArr[][] = new int[8][8];
chessArr[1][1] = 1;
chessArr[2][2] = 2;
// 遍歷原始數(shù)組,獲得不等于 0 的個(gè)數(shù),并輸出原始數(shù)組
int sum = 0;
System.out.println("原始數(shù)組:");
for (int[] ints : chessArr) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
if (anInt != 0){
sum++;
}
}
System.out.println();
}
System.out.println(sum);
// 創(chuàng)建稀疏數(shù)組并賦值
int sparseArray[][] = new int[sum+1][3];
int row = chessArr.length; // 原數(shù)組的行數(shù)
int col = chessArr[0].length; // 原數(shù)組的列數(shù)
sparseArray[0][0] = row;
sparseArray[0][1] = col;
sparseArray[0][2] = sum;
// 遍歷原始數(shù)組并賦值給稀疏數(shù)組
int count = 0; // count 為計(jì)數(shù)器
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++) {
if (chessArr[i][j] != 0) {
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chessArr[i][j];
}
}
}
// 輸出得到的稀疏數(shù)組
System.out.println("稀疏數(shù)組為:");
for (int i = 0 ;i < sparseArray.length;i++) {
System.out.println(sparseArray[i][0]+"\t"+sparseArray[i][1]+"\t"+sparseArray[i][2]);
}
}
}
2.實(shí)現(xiàn)二維數(shù)組轉(zhuǎn)稀疏數(shù)組的步驟
- 根據(jù)稀疏數(shù)組的第一行創(chuàng)建新的二維數(shù)組
- 遍歷稀疏數(shù)組,將row col val 賦給新的二維數(shù)組
代碼實(shí)現(xiàn):
public class SparseArray {
public static void main(String[] args) {
...... // 接上一段代碼
// 輸出得到的稀疏數(shù)組
System.out.println("稀疏數(shù)組為:");
for (int i = 0 ;i < sparseArray.length;i++) {
System.out.println(sparseArray[i][0]+"\t"+sparseArray[i][1]+"\t"+sparseArray[i][2]);
}
// 創(chuàng)建新的二維數(shù)組
int newChessArray[][] = new int[sparseArray[0][0]][sparseArray[0][1]];
for (int i = 1 ;i < sparseArray.length;i++) {
newChessArray[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
}
// 輸出新的二維數(shù)組
System.out.println("恢復(fù)后的二維數(shù)組是:");
for (int[] ints : newChessArray) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
}
}
3.將稀疏數(shù)組寫入磁盤
public static void main(String[] args) {
........
// 將稀疏數(shù)組存入磁盤
FileOutputStream fw = null;
try {
fw = new FileOutputStream("sparseArray.txt");
// 遍歷寫入磁盤
for (int i = 0; i < sparseArray.length; i++) {
for (int j = 0; j < 3; j++) {
fw.write(sparseArray[i][j]);
}
}
fw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null){
fw.close();
}
}
}
4.從磁盤讀取稀疏數(shù)組
public static void main(String[] args) {
.......
// 從磁盤讀取稀疏數(shù)組
FileInputStream fi = null;
int newSparseArray[][] = new int[sum+1][3]; // sum 指前面二維數(shù)組有值的個(gè)數(shù)
try {
fi = new FileInputStream("sparseArray.txt");
for (int i = 0;i < newSparseArray.length;i++) {
for (int j = 0;j < 3;j++){
newSparseArray[i][j] = fi.read();
}
}
} catch (FileNotFoundException e){
e.printStackTrace();
} finally {
if (fi != null){
fi.close();
}
}
}
到此這篇關(guān)于淺談Java數(shù)據(jù)結(jié)構(gòu)之稀疏數(shù)組知識(shí)總結(jié)的文章就介紹到這了,更多相關(guān)Java稀疏數(shù)組內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)在普通類中注入service或mapper
這篇文章主要介紹了java實(shí)現(xiàn)在普通類中注入service或mapper的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
feign的ribbon超時(shí)配置和hystrix的超時(shí)配置說明
這篇文章主要介紹了feign的ribbon超時(shí)配置和hystrix的超時(shí)配置說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
詳解SpringMVC中設(shè)置靜態(tài)資源不被攔截的問題
這篇文章主要介紹了詳解SpringMVC中設(shè)置靜態(tài)資源不被攔截的問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
MyBatisPlus利用Service實(shí)現(xiàn)獲取數(shù)據(jù)列表
這篇文章主要為大家詳細(xì)介紹了怎樣使用 IServer 提供的 list 方法查詢多條數(shù)據(jù),這些方法將根據(jù)查詢條件獲取多條數(shù)據(jù),感興趣的可以了解一下2022-06-06
MyBatis-Plus插件機(jī)制及通用Service新功能
這篇文章主要介紹了MyBatis-Plus插件機(jī)制以及通用Service、新功能,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07

