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

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

代碼實(shí)現(xiàn)
創(chuàng)建模擬結(jié)點(diǎn)的類 提供構(gòu)造方法和toString
/**
* 模擬結(jié)點(diǎn)
*/
public class SingleNode {
/**
* @row 行號(hào)
* @column 列號(hào)
* @num 值
*/
public int row;
public int colunm;
public int num;
/**
*next域:指向下一個(gè)結(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) {
//如果這個(gè)結(jié)點(diǎn)的next域?yàn)榭站吞鰓hile循環(huán)
break;
}
//將下一個(gè)結(jié)點(diǎn)賦值給temp
temp = temp.next;
}
temp.next= SingleNode;
}
/**
* 遍歷單項(xiàng)鏈表
*/
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)中存儲(chǔ)著原數(shù)組的行數(shù)和列數(shù)
//通過這兩個(gè)值創(chuàng)建二維數(shù)組
int[][] array = new int[headSingleNode.row][headSingleNode.colunm];
while (true){
if (temp.next == null){
break;
}
temp = temp.next;
//每遍歷一個(gè)結(jié)點(diǎn)就還原一個(gè)數(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)酱鎯?chǔ)
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時(shí)視為有效值就創(chuàng)建一個(gè)結(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如何實(shí)現(xiàn)將二維數(shù)組轉(zhuǎn)化為鏈?zhǔn)絻?chǔ)存的文章就介紹到這了,更多相關(guān)Java二維數(shù)組轉(zhuǎn)鏈?zhǔn)絻?chǔ)存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot實(shí)現(xiàn)根據(jù)用戶ID切換動(dòng)態(tài)數(shù)據(jù)源
在很多具體應(yīng)用場(chǎng)景中,我們需要用到動(dòng)態(tài)數(shù)據(jù)源的情況,比如多租戶的場(chǎng)景,系統(tǒng)登錄時(shí)需要根據(jù)用戶信息切換到用戶對(duì)應(yīng)的數(shù)據(jù)庫。這篇文章主要介紹了SpringBoot根據(jù)用戶ID實(shí)現(xiàn)切換動(dòng)態(tài)數(shù)據(jù)源的示例代碼,感興趣的可以了解一下2021-12-12
java開源項(xiàng)目jeecgboot的超詳細(xì)解析
JeecgBoot是一款基于BPM的低代碼平臺(tái),下面這篇文章主要給大家介紹了關(guān)于java開源項(xiàng)目jeecgboot的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
java利用pdfbox+poi往pdf插入數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于java利用pdfbox+poi如何往pdf插入數(shù)據(jù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
關(guān)于SpringBoot的自動(dòng)裝配原理詳解
這篇文章主要介紹了關(guān)于SpringBoot的自動(dòng)裝配原理詳解,Spring?Boot自動(dòng)裝配原理是指Spring?Boot在啟動(dòng)時(shí)自動(dòng)掃描項(xiàng)目中的依賴關(guān)系,根據(jù)依賴關(guān)系自動(dòng)配置相應(yīng)的Bean,從而簡(jiǎn)化了Spring應(yīng)用的配置過程,需要的朋友可以參考下2023-07-07
Spring事務(wù)失效的場(chǎng)景梳理總結(jié)
實(shí)際項(xiàng)目開發(fā)中,如果涉及到多張表操作時(shí),為了保證業(yè)務(wù)數(shù)據(jù)的一致性,大家一般都會(huì)采用事務(wù)機(jī)制,好多小伙伴可能只是簡(jiǎn)單了解一下,遇到事務(wù)失效的情況,便會(huì)無從下手,下面這篇文章主要給大家介紹了關(guān)于Spring事務(wù)失效場(chǎng)景的相關(guān)資料,需要的朋友可以參考下2023-02-02

