淺談Java數據結構之稀疏數組知識總結
更新時間:2021年05月25日 08:53:27 作者:xdmx
今天帶大家了解一下Java稀疏數組的相關知識,文中有非常詳細的介紹及代碼示例,對正在學習java的小伙伴們有很好地幫助,需要的朋友可以參考下
稀疏數組
- 當一個數組中的元素大多為0或者相同元素的時候,可以用稀疏數組來壓縮
- 稀疏數組只記錄 行row 列col 值value
將下列的二維數組轉為稀疏數組,如下兩圖所示
1.實現二維數組轉為稀疏數組的步驟:
- 遍歷數組,得到數組中 不為0的個數,并記錄為sum,作為稀疏數組第0行的 value
- 遍歷數組,將數組中不為0的數的行和列和值分別寫入稀疏數組的 row col val 中
代碼實現:
public class SparseArray { public static void main(String[] args) { // 創(chuàng)建一個原始二維數組 // 0表示沒有棋子,1,2各表示一種棋 int chessArr[][] = new int[8][8]; chessArr[1][1] = 1; chessArr[2][2] = 2; // 遍歷原始數組,獲得不等于 0 的個數,并輸出原始數組 int sum = 0; System.out.println("原始數組:"); 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)建稀疏數組并賦值 int sparseArray[][] = new int[sum+1][3]; int row = chessArr.length; // 原數組的行數 int col = chessArr[0].length; // 原數組的列數 sparseArray[0][0] = row; sparseArray[0][1] = col; sparseArray[0][2] = sum; // 遍歷原始數組并賦值給稀疏數組 int count = 0; // count 為計數器 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]; } } } // 輸出得到的稀疏數組 System.out.println("稀疏數組為:"); for (int i = 0 ;i < sparseArray.length;i++) { System.out.println(sparseArray[i][0]+"\t"+sparseArray[i][1]+"\t"+sparseArray[i][2]); } } }
2.實現二維數組轉稀疏數組的步驟
- 根據稀疏數組的第一行創(chuàng)建新的二維數組
- 遍歷稀疏數組,將row col val 賦給新的二維數組
代碼實現:
public class SparseArray { public static void main(String[] args) { ...... // 接上一段代碼 // 輸出得到的稀疏數組 System.out.println("稀疏數組為:"); for (int i = 0 ;i < sparseArray.length;i++) { System.out.println(sparseArray[i][0]+"\t"+sparseArray[i][1]+"\t"+sparseArray[i][2]); } // 創(chuàng)建新的二維數組 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]; } // 輸出新的二維數組 System.out.println("恢復后的二維數組是:"); for (int[] ints : newChessArray) { for (int anInt : ints) { System.out.print(anInt+"\t"); } System.out.println(); } } }
3.將稀疏數組寫入磁盤
public static void main(String[] args) { ........ // 將稀疏數組存入磁盤 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.從磁盤讀取稀疏數組
public static void main(String[] args) { ....... // 從磁盤讀取稀疏數組 FileInputStream fi = null; int newSparseArray[][] = new int[sum+1][3]; // sum 指前面二維數組有值的個數 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(); } } }
到此這篇關于淺談Java數據結構之稀疏數組知識總結的文章就介紹到這了,更多相關Java稀疏數組內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
feign的ribbon超時配置和hystrix的超時配置說明
這篇文章主要介紹了feign的ribbon超時配置和hystrix的超時配置說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09詳解SpringMVC中設置靜態(tài)資源不被攔截的問題
這篇文章主要介紹了詳解SpringMVC中設置靜態(tài)資源不被攔截的問題,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02