您现在的位置是:主页 > news > 建筑工程网上保健网站/百度竞价调价软件
建筑工程网上保健网站/百度竞价调价软件
admin2025/5/3 20:45:07【news】
简介建筑工程网上保健网站,百度竞价调价软件,抖店推广,苏州产品推广公司韩顺平数据结构之稀疏数组 **需求:**将一个二维数组转换成一个稀疏数组,并且将稀疏数组再一次的返回为二维数组 二维数组转换成一个稀疏数组思路分析: 遍历原本的二维数组,得到有效的数据个数sum根据sum就可以创建稀疏数组spararr [sum1] [3]将二维数组的有效数据读取到稀疏…
建筑工程网上保健网站,百度竞价调价软件,抖店推广,苏州产品推广公司韩顺平数据结构之稀疏数组
**需求:**将一个二维数组转换成一个稀疏数组,并且将稀疏数组再一次的返回为二维数组
二维数组转换成一个稀疏数组思路分析:
遍历原本的二维数组,得到有效的数据个数sum根据sum就可以创建稀疏数组spararr [sum1] [3]将二维数组的有效数据读取到稀疏…
韩顺平数据结构之稀疏数组
**需求:**将一个二维数组转换成一个稀疏数组,并且将稀疏数组再一次的返回为二维数组
二维数组转换成一个稀疏数组思路分析:
- 遍历原本的二维数组,得到有效的数据个数sum
- 根据sum就可以创建稀疏数组spararr [sum+1] [3]
- 将二维数组的有效数据读取到稀疏数组
稀疏数组转换成原本数组的思路分析:
- 先读取到稀疏数组的第一行,根据第一行的数据,创建原本的二维数组,
- 在读取稀疏数组后的几行数据,并且赋给原始的二维数据即可
package com.ywystu.sparsearray;/*** @author 是狸猫啊!* @Version 1.0*/
public class SparseArr {public static void main(String[] args) {//先定义一个二维数组int[][] chess = new int[11][11];//往数组里面赋值chess[1][2] = 1;chess[2][3] = 2;for (int[] row :chess) {for (int cal :row) {System.out.print(cal+"\t");}System.out.println();}//创建一个稀疏数组int sum = 0;//判断原来数据中不为0的个数for (int i = 0; i < chess.length; i++) {for (int j = 0; j < chess[i].length; j++) {if (chess[i][j] != 0){sum++;}}}//他有几个不为0的数,就应该为sum+1行int[][] spaseArr = new int[sum + 1][3];spaseArr[0][0] = 11;spaseArr[0][1] = 11;spaseArr[0][2] = sum;int count = 0;//对sparsearr的值进行判断for (int i = 0; i < chess.length; i++) {for (int j = 0; j < chess[i].length; j++) {if (chess[i][j] != 0){count++;spaseArr[count][0] = i;spaseArr[count][1] = j;spaseArr[count][2] = chess[i][j];}}}//遍历输出稀疏数组的值System.out.println("得到的稀疏数组为:");for (int[] row :spaseArr) {for (int ical :row) {System.out.print(ical + "\t");}System.out.println();}//将稀疏数组返回为二维数组System.out.println("返回后的结果为");int[][] chess1 = new int[spaseArr[0][0]][spaseArr[0][1]];//进行数据的填充//注意:1.不应该从0开始遍历,因为第一行的数据是数组的结构for (int i = 1; i < spaseArr.length; i++) {chess1[spaseArr[i][0]][spaseArr[i][1]] = spaseArr[i][2];}for (int i = 0; i < chess1.length; i++) {for (int j = 0; j < chess1[i].length; j++) {System.out.print(chess1[i][j] + "\t");}System.out.println();}}
}