首页 > 其他分享 >Demo48_还原稀疏数组

Demo48_还原稀疏数组

时间:2022-10-26 11:36:43浏览次数:74  
标签:AA BB Demo48 int System 稀疏 数组 println out

//还原稀疏数组
package com.HuanXin.array_6;

public class Demo09_01 {
public static void main(String[] args) {
int[][]AA=new int[7][7];
AA[1][1]=1;
AA[1][2]=2;
for (int i = 0; i <AA.length ; i++) {
for (int j = 0; j <AA[i].length ; j++) {
System.out.print(AA[i][j]+" ");
}
System.out.println();
}
int num=0;
for (int i = 0; i <AA.length ; i++) {
for (int j = 0; j <AA[i].length ; j++) {
if (AA[i][j]!=0){
num++;
}
}
}
System.out.println("原始稀疏数组的有效值="+num);

int num1=0;
int [][]BB=new int[num+1][3];
BB[0][0]=7;
BB[0][1]=7;
BB[0][2]=2;
for (int i = 0; i <AA.length ; i++) {
for (int j = 0; j <AA[i].length ; j++) {
if (AA[i][j]!=0){
num1++;
BB[num1][0]=i;
BB[num1][1]=j;
BB[num1][2]=AA[i][j];
}
}
}
System.out.println("输出稀疏数组");
for (int i = 0; i <BB.length ; i++) {
System.out.println(BB[i][0]+" "+
BB[i][1]+" "+
BB[i][2]+" ");
}
System.out.println("还原稀疏数组");
int[][]CC=new int[BB[0][0]][BB[0][1]];
for (int i = 1; i <BB.length ; i++) {
CC[BB[i][0]][BB[i][1]]=BB[i][2];
}
for (int i = 0; i <CC.length ; i++) {
for (int j = 0; j <CC[i].length ; j++) {
System.out.print(CC[i][j]+" ");
}
System.out.println();
}
}
}

 

标签:AA,BB,Demo48,int,System,稀疏,数组,println,out
From: https://www.cnblogs.com/CHX249/p/16827626.html

相关文章