利用Java如何實現(xiàn)將二維數(shù)組轉化為鏈式儲存
鏈式存儲結構
鏈式存儲結構的線性表將采用一組任意的存儲單元存放線性表中的數(shù)據(jù)元素。由于不需要按順序存儲,鏈表在插入、刪除數(shù)據(jù)元素時比順序存儲要快,但是在查找一個節(jié)點時則要比順序存儲要慢。
使用鏈式存儲可以克服順序線性表需要預先知道數(shù)據(jù)大小的缺點,鏈表結構可以充分利用內(nèi)存空間,實現(xiàn)靈活的內(nèi)存動態(tài)管理。但是鏈式存儲失去了數(shù)組隨機存取的特點,同時增加了節(jié)點的指針域,空間開銷較大。
下圖就是最簡單最一般的單向鏈表:

代碼思路
將二維數(shù)組壓縮成鏈式存儲大體思路與數(shù)組壓縮成稀疏數(shù)組相似
這里我將鏈表的頭節(jié)點儲存二維數(shù)組的總行數(shù)、列數(shù)和有效值個
數(shù) 頭結點之后的每個結點存儲有效值的下標和值 轉化思路如下圖所示

代碼實現(xiàn)
創(chuàng)建模擬結點的類 提供構造方法和toString
/**
* 模擬結點
*/
public class SingleNode {
/**
* @row 行號
* @column 列號
* @num 值
*/
public int row;
public int colunm;
public int num;
/**
*next域:指向下一個結點
*/
public SingleNode next;
public SingleNode(int row, int colunm, int num) {
this.row = row;
this.colunm = colunm;
this.num = num;
}
@Override
public String toString() {
return "SingleNode{" + "row=" + row + ", colunm=" + colunm + ", num=" + num + '}';
}
}
創(chuàng)建模擬鏈表類 提供添加結點、遍歷鏈表和還原數(shù)組等方法
public class SingleLinkList {
//頭節(jié)點
private SingleNode headSingleNode;
//通過構造行數(shù)初始化頭節(jié)點
public SingleLinkList(SingleNode headSingleNode) {
this.headSingleNode = headSingleNode;
}
/**
* 添加結點(追加在鏈表的尾端)
*/
public void add(SingleNode SingleNode){
SingleNode temp = headSingleNode;
while(true){
if (temp.next == null) {
//如果這個結點的next域為空就跳出while循環(huán)
break;
}
//將下一個結點賦值給temp
temp = temp.next;
}
temp.next= SingleNode;
}
/**
* 遍歷單項鏈表
*/
public void show(){
if (headSingleNode.next == null) {
System.out.println("當前鏈表為空");
return;
}
SingleNode temp = headSingleNode;
while (true){
if (temp.next == null){
break;
}
temp = temp.next;
System.out.println(temp);
}
}
/**
* 將鏈表還原成二維數(shù)組
* @return array還原后的二維數(shù)組
*/
public int[][] backToArray(){
SingleNode temp = headSingleNode;
//頭結點中存儲著原數(shù)組的行數(shù)和列數(shù)
//通過這兩個值創(chuàng)建二維數(shù)組
int[][] array = new int[headSingleNode.row][headSingleNode.colunm];
while (true){
if (temp.next == null){
break;
}
temp = temp.next;
//每遍歷一個結點就還原一個數(shù)據(jù)
array[temp.row][temp.colunm] = temp.num;
}
return array;
}
}
public class ArrayToLink {
public static void main(String[] args) {
int[][] array = new int[4][5];
//初始化二維數(shù)組
array[0][2] = 1;
array[1][1] = 2;
array[2][3] = 3;
System.out.println("========普通數(shù)組========");
//遍歷二維數(shù)組
int count = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (array[i][j]!=0){
count++;
}
System.out.print(array[i][j] + " ");
}
System.out.println();
}
//將數(shù)組轉化成鏈式存儲
SingleLinkList list = new SingleLinkList(new SingleNode(array.length,array[0].length,count));
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (array[i][j] != 0){
//當數(shù)字不為0時視為有效值就創(chuàng)建一個結點并添加到鏈表尾部
list.add(new SingleNode(i,j,array[i][j]));
}
}
}
System.out.println("========轉化后的鏈表========");
//遍歷單向鏈表
list.show();
int[][] returnArray = list.backToArray();
//遍歷還原后的二維數(shù)組
System.out.println("========還原后的數(shù)組========");
for (int i = 0; i < returnArray.length; i++) {
for (int j = 0; j < returnArray[i].length; j++) {
System.out.print(returnArray[i][j] + " ");
}
System.out.println();
}
}
}
輸出結果

總結
到此這篇關于利用Java如何實現(xiàn)將二維數(shù)組轉化為鏈式儲存的文章就介紹到這了,更多相關Java二維數(shù)組轉鏈式儲存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Springboot實現(xiàn)根據(jù)用戶ID切換動態(tài)數(shù)據(jù)源
在很多具體應用場景中,我們需要用到動態(tài)數(shù)據(jù)源的情況,比如多租戶的場景,系統(tǒng)登錄時需要根據(jù)用戶信息切換到用戶對應的數(shù)據(jù)庫。這篇文章主要介紹了SpringBoot根據(jù)用戶ID實現(xiàn)切換動態(tài)數(shù)據(jù)源的示例代碼,感興趣的可以了解一下2021-12-12
java利用pdfbox+poi往pdf插入數(shù)據(jù)
這篇文章主要給大家介紹了關于java利用pdfbox+poi如何往pdf插入數(shù)據(jù)的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-02-02

