java稀疏數(shù)組的示例代碼
稀疏組織
- 當(dāng)一個數(shù)組中大部分元素為0,或者為同一個值的數(shù)組時,可以用稀疏數(shù)組來保存該數(shù)組
- 稀疏數(shù)組,記錄一共有幾行幾列,有多少個不同值
- 把具有不同值的元素和行里了及值記錄在一個小規(guī)模的數(shù)組中,從而縮小程序的規(guī)模!
我們定義一下原始數(shù)組:
原始數(shù)組如下: 0 0 3 0 0 0 0 0 0 4 0 0 0 5 0 0 0 6 0 0 0 0 0 0 0
可以看出,這個數(shù)組大部分都是0,我們可以把這個數(shù)組轉(zhuǎn)化為稀疏數(shù)組
稀疏數(shù)組第一行存放的分別是總行數(shù),總列數(shù)和存放的數(shù)據(jù)總數(shù)
//因?yàn)閿?shù)組的下標(biāo)是從0開始的,所以可以看出,第一行的第三個數(shù),用下標(biāo)表示,實(shí)際上是數(shù)組[0][2] 5 5 4 0 2 3 1 4 4 2 3 5 3 2 6
下面看一下如何實(shí)現(xiàn)這種稀疏數(shù)組,又是如何把稀疏數(shù)組還原成
代碼示例:
package com.ling.array;
public class ArrayDemo11 {
public static void main(String[] args) {
int[][] array=new int[5][5];
array[0][2]=3;
array[1][4]=4;
array[2][3]=5;
array[3][2]=6;
System.out.println("原始數(shù)組如下:");
for (int[] ints : array) {
for (int anInt : ints) {
System.out.print(anInt+" ");
}
System.out.println();
}
System.out.println("行"+"\t"+"列"+"\t"+"存放的數(shù)據(jù)"+"\t");
//稀疏數(shù)組第一行存放的分別是總行數(shù),總列數(shù)和存放的數(shù)據(jù)總數(shù)
// System.out.println(5+"\t"+5+"\t"+4);
// System.out.println(1+"\t"+3+"\t"+3);
// System.out.println(2+"\t"+5+"\t"+4);
// System.out.println(3+"\t"+4+"\t"+5);
// System.out.println(4+"\t"+3+"\t"+6);
int sum=0;
for (int i = 0; i <5 ; i++) {
for (int j = 0; j <5 ; j++) {
if (array[i][j]!=0){
sum++;
}
}
}
int[][] arr2=new int[sum+1][3];
//這個二維數(shù)組的第一行是確定的
arr2[0][0]=5;
arr2[0][1]=5;
arr2[0][2]=sum;
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++;
arr2[count][0]=i;
arr2[count][1]=j;
arr2[count][2]=array[i][j];
}
}
}
System.out.println("輸出稀疏數(shù)組");
for (int[] ints : arr2) {
for (int anInt : ints) {
System.out.print(anInt+" ");
}
System.out.println();
}
}
}
輸出:
輸出原始的數(shù)組
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
有效值的個數(shù):2
輸出稀疏數(shù)組:
11 11 2
1 2 1
3 2 2
=========分========割==========線=============
我們也可以把稀疏數(shù)組進(jìn)行一個還原
打印還原后的數(shù)組
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
以上就是關(guān)于稀疏數(shù)組的講解,有不理解的可以跟著代碼試一下,然后自己創(chuàng)建一個數(shù)組,并把數(shù)組轉(zhuǎn)換為稀疏數(shù)組!再試著轉(zhuǎn)換回來。
到此這篇關(guān)于java稀疏數(shù)組的文章就介紹到這了,更多相關(guān)java稀疏數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis plus關(guān)閉駝峰命名的四種方法(防止出現(xiàn)查詢?yōu)镹ull)
這篇文章主要介紹了Mybatis plus關(guān)閉駝峰命名的四種方法(防止出現(xiàn)查詢?yōu)镹ull),數(shù)據(jù)庫的字段命名方式為使用下劃線連接,對應(yīng)的實(shí)體類應(yīng)該是駝峰命名方式,而我使用的是和數(shù)據(jù)庫同樣的命名方式,需要的朋友可以參考下2022-01-01
Java接口方法默認(rèn)靜態(tài)實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Java接口方法默認(rèn)靜態(tài)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
java實(shí)現(xiàn)微信小程序登錄態(tài)維護(hù)的示例代碼
本篇文章主要介紹了java實(shí)現(xiàn)微信小程序登錄態(tài)維護(hù)的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-09-09
Failed to execute goal org...的解決辦法
這篇文章主要介紹了Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06
淺談JSON的數(shù)據(jù)交換、緩存問題和同步問題
這篇文章主要介紹了淺談JSON的數(shù)據(jù)交換、緩存問題和同步問題,具有一定借鑒價值,需要的朋友可以參考下2017-12-12
Java枚舉_動力節(jié)點(diǎn)Java學(xué)院整理
enum 的全稱為 enumeration, 是 JDK 5 中引入的新特性,存放在 java.lang 包中。這篇文章給大家介紹Java枚舉相關(guān)知識,需要的的朋友參考下2017-04-04

