利用Java如何實現(xiàn)將二維數(shù)組轉(zhuǎn)化為鏈?zhǔn)絻Υ?/h1>
更新時間:2021年12月15日 09:55:43 作者:HairLossException
鏈?zhǔn)浇Y(jié)構(gòu)不要求邏輯上相鄰的節(jié)點(diǎn)在物理位置上也相鄰,節(jié)點(diǎn)間的邏輯關(guān)系是由附加的指針字段表示的,通常借助于程序設(shè)計中的指針結(jié)構(gòu)來實現(xiàn),這篇文章主要給大家介紹了關(guān)于利用Java如何實現(xiàn)將二維數(shù)組轉(zhuǎn)化為鏈?zhǔn)絻Υ娴南嚓P(guān)資料,需要的朋友可以參考下
鏈?zhǔn)酱鎯Y(jié)構(gòu)
鏈?zhǔn)酱鎯Y(jié)構(gòu)的線性表將采用一組任意的存儲單元存放線性表中的數(shù)據(jù)元素。由于不需要按順序存儲,鏈表在插入、刪除數(shù)據(jù)元素時比順序存儲要快,但是在查找一個節(jié)點(diǎn)時則要比順序存儲要慢。
使用鏈?zhǔn)酱鎯梢钥朔樞蚓€性表需要預(yù)先知道數(shù)據(jù)大小的缺點(diǎn),鏈表結(jié)構(gòu)可以充分利用內(nèi)存空間,實現(xiàn)靈活的內(nèi)存動態(tài)管理。但是鏈?zhǔn)酱鎯κチ藬?shù)組隨機(jī)存取的特點(diǎn),同時增加了節(jié)點(diǎn)的指針域,空間開銷較大。
下圖就是最簡單最一般的單向鏈表:

代碼思路
將二維數(shù)組壓縮成鏈?zhǔn)酱鎯Υ篌w思路與數(shù)組壓縮成稀疏數(shù)組相似
這里我將鏈表的頭節(jié)點(diǎn)儲存二維數(shù)組的總行數(shù)、列數(shù)和有效值個
數(shù) 頭結(jié)點(diǎn)之后的每個結(jié)點(diǎn)存儲有效值的下標(biāo)和值 轉(zhuǎn)化思路如下圖所示

代碼實現(xiàn)
創(chuàng)建模擬結(jié)點(diǎn)的類 提供構(gòu)造方法和toString
/**
* 模擬結(jié)點(diǎn)
*/
public class SingleNode {
/**
* @row 行號
* @column 列號
* @num 值
*/
public int row;
public int colunm;
public int num;
/**
*next域:指向下一個結(jié)點(diǎn)
*/
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)建模擬鏈表類 提供添加結(jié)點(diǎn)、遍歷鏈表和還原數(shù)組等方法
public class SingleLinkList {
//頭節(jié)點(diǎn)
private SingleNode headSingleNode;
//通過構(gòu)造行數(shù)初始化頭節(jié)點(diǎn)
public SingleLinkList(SingleNode headSingleNode) {
this.headSingleNode = headSingleNode;
}
/**
* 添加結(jié)點(diǎn)(追加在鏈表的尾端)
*/
public void add(SingleNode SingleNode){
SingleNode temp = headSingleNode;
while(true){
if (temp.next == null) {
//如果這個結(jié)點(diǎn)的next域為空就跳出while循環(huán)
break;
}
//將下一個結(jié)點(diǎn)賦值給temp
temp = temp.next;
}
temp.next= SingleNode;
}
/**
* 遍歷單項鏈表
*/
public void show(){
if (headSingleNode.next == null) {
System.out.println("當(dāng)前鏈表為空");
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;
//頭結(jié)點(diǎn)中存儲著原數(shù)組的行數(shù)和列數(shù)
//通過這兩個值創(chuàng)建二維數(shù)組
int[][] array = new int[headSingleNode.row][headSingleNode.colunm];
while (true){
if (temp.next == null){
break;
}
temp = temp.next;
//每遍歷一個結(jié)點(diǎn)就還原一個數(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ù)組轉(zhuǎn)化成鏈?zhǔn)酱鎯?
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){
//當(dāng)數(shù)字不為0時視為有效值就創(chuàng)建一個結(jié)點(diǎn)并添加到鏈表尾部
list.add(new SingleNode(i,j,array[i][j]));
}
}
}
System.out.println("========轉(zhuǎn)化后的鏈表========");
//遍歷單向鏈表
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();
}
}
}
輸出結(jié)果

總結(jié)
到此這篇關(guān)于利用Java如何實現(xiàn)將二維數(shù)組轉(zhuǎn)化為鏈?zhǔn)絻Υ娴奈恼戮徒榻B到這了,更多相關(guān)Java二維數(shù)組轉(zhuǎn)鏈?zhǔn)絻Υ鎯?nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
-
Springboot實現(xiàn)根據(jù)用戶ID切換動態(tài)數(shù)據(jù)源
在很多具體應(yīng)用場景中,我們需要用到動態(tài)數(shù)據(jù)源的情況,比如多租戶的場景,系統(tǒng)登錄時需要根據(jù)用戶信息切換到用戶對應(yīng)的數(shù)據(jù)庫。這篇文章主要介紹了SpringBoot根據(jù)用戶ID實現(xiàn)切換動態(tài)數(shù)據(jù)源的示例代碼,感興趣的可以了解一下 2021-12-12
-
java利用pdfbox+poi往pdf插入數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于java利用pdfbox+poi如何往pdf插入數(shù)據(jù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下 2022-02-02
最新評論
鏈?zhǔn)酱鎯Y(jié)構(gòu)
鏈?zhǔn)酱鎯Y(jié)構(gòu)的線性表將采用一組任意的存儲單元存放線性表中的數(shù)據(jù)元素。由于不需要按順序存儲,鏈表在插入、刪除數(shù)據(jù)元素時比順序存儲要快,但是在查找一個節(jié)點(diǎn)時則要比順序存儲要慢。
使用鏈?zhǔn)酱鎯梢钥朔樞蚓€性表需要預(yù)先知道數(shù)據(jù)大小的缺點(diǎn),鏈表結(jié)構(gòu)可以充分利用內(nèi)存空間,實現(xiàn)靈活的內(nèi)存動態(tài)管理。但是鏈?zhǔn)酱鎯κチ藬?shù)組隨機(jī)存取的特點(diǎn),同時增加了節(jié)點(diǎn)的指針域,空間開銷較大。
下圖就是最簡單最一般的單向鏈表:
代碼思路
將二維數(shù)組壓縮成鏈?zhǔn)酱鎯Υ篌w思路與數(shù)組壓縮成稀疏數(shù)組相似
這里我將鏈表的頭節(jié)點(diǎn)儲存二維數(shù)組的總行數(shù)、列數(shù)和有效值個
數(shù) 頭結(jié)點(diǎn)之后的每個結(jié)點(diǎn)存儲有效值的下標(biāo)和值 轉(zhuǎn)化思路如下圖所示
代碼實現(xiàn)
創(chuàng)建模擬結(jié)點(diǎn)的類 提供構(gòu)造方法和toString
/** * 模擬結(jié)點(diǎn) */ public class SingleNode { /** * @row 行號 * @column 列號 * @num 值 */ public int row; public int colunm; public int num; /** *next域:指向下一個結(jié)點(diǎn) */ 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)建模擬鏈表類 提供添加結(jié)點(diǎn)、遍歷鏈表和還原數(shù)組等方法
public class SingleLinkList { //頭節(jié)點(diǎn) private SingleNode headSingleNode; //通過構(gòu)造行數(shù)初始化頭節(jié)點(diǎn) public SingleLinkList(SingleNode headSingleNode) { this.headSingleNode = headSingleNode; } /** * 添加結(jié)點(diǎn)(追加在鏈表的尾端) */ public void add(SingleNode SingleNode){ SingleNode temp = headSingleNode; while(true){ if (temp.next == null) { //如果這個結(jié)點(diǎn)的next域為空就跳出while循環(huán) break; } //將下一個結(jié)點(diǎn)賦值給temp temp = temp.next; } temp.next= SingleNode; } /** * 遍歷單項鏈表 */ public void show(){ if (headSingleNode.next == null) { System.out.println("當(dāng)前鏈表為空"); 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; //頭結(jié)點(diǎn)中存儲著原數(shù)組的行數(shù)和列數(shù) //通過這兩個值創(chuàng)建二維數(shù)組 int[][] array = new int[headSingleNode.row][headSingleNode.colunm]; while (true){ if (temp.next == null){ break; } temp = temp.next; //每遍歷一個結(jié)點(diǎn)就還原一個數(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ù)組轉(zhuǎn)化成鏈?zhǔn)酱鎯? 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){ //當(dāng)數(shù)字不為0時視為有效值就創(chuàng)建一個結(jié)點(diǎn)并添加到鏈表尾部 list.add(new SingleNode(i,j,array[i][j])); } } } System.out.println("========轉(zhuǎn)化后的鏈表========"); //遍歷單向鏈表 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(); } } }
輸出結(jié)果
總結(jié)
到此這篇關(guān)于利用Java如何實現(xiàn)將二維數(shù)組轉(zhuǎn)化為鏈?zhǔn)絻Υ娴奈恼戮徒榻B到這了,更多相關(guān)Java二維數(shù)組轉(zhuǎn)鏈?zhǔn)絻Υ鎯?nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot實現(xiàn)根據(jù)用戶ID切換動態(tài)數(shù)據(jù)源
在很多具體應(yīng)用場景中,我們需要用到動態(tài)數(shù)據(jù)源的情況,比如多租戶的場景,系統(tǒng)登錄時需要根據(jù)用戶信息切換到用戶對應(yīng)的數(shù)據(jù)庫。這篇文章主要介紹了SpringBoot根據(jù)用戶ID實現(xiàn)切換動態(tài)數(shù)據(jù)源的示例代碼,感興趣的可以了解一下2021-12-12java利用pdfbox+poi往pdf插入數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于java利用pdfbox+poi如何往pdf插入數(shù)據(jù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-02-02