首页 > 其他分享 >稀疏数组

稀疏数组

时间:2023-01-06 13:35:04浏览次数:33  
标签:count integers sparseArray 稀疏 source 数组 Integer

用途:将普通数组转为稀疏数组来达到节省空间的目的

介绍:

代码:

 

import cn.hutool.core.util.ObjectUtil;
import org.junit.Test;

public class SparseArrayTest {
    @Test
    public void test() {
        Integer[][] integers = new Integer[6][7];
        integers[0][3] = 22;
        integers[0][6] = 15;
        integers[1][1] = 11;
        integers[1][5] = 17;
        integers[2][3] = -6;
        integers[3][5] = 39;
        integers[4][0] = 91;
        integers[5][2] = 28;
        System.out.println("~~~原始数组~~~");
        SparseArray.showArray(integers);
        Integer[][] sparseArray = SparseArray.toSparseArray(integers);
        System.out.println("~~~稀疏数组~~~");
        SparseArray.showArray(sparseArray);
        Integer[][] ordinaryArray = SparseArray.toOrdinaryArray(sparseArray);
        System.out.println("~~~普通数组~~~");
        SparseArray.showArray(ordinaryArray);
    }
}

class SparseArray {

    /**
     * 普通数组转为稀疏数组
     * @param source
     * @return
     */
    public static Integer[][] toSparseArray(Integer[][] source) {
        //对source的有效数据进行计数
        int count = 0;
        for (int i = 0; i < source.length; i++) {
            for (int j = 0; j < source[i].length ; j++) {
                if (ObjectUtil.isNotNull(source[i][j])) {
                    count++;
                }
            }
        }
        //创建稀疏数组
        Integer[][] sparseArray = new Integer[count + 1][3];
        //设置稀疏数组第一行的数据
        sparseArray[0][0] = source.length;
        sparseArray[0][1] = source[0].length;
        sparseArray[0][2] = count;
        //将有效数据放进稀疏数组
        count = 0;
        for (int i = 0; i < source.length; i++) {
            for (int j = 0; j < source[i].length ; j++) {
                if (ObjectUtil.isNotNull(source[i][j])) {
                    count ++;
                    sparseArray[count][0] = i;
                    sparseArray[count][1] = j;
                    sparseArray[count][2] = source[i][j];
                }
            }
        }
        return sparseArray;
    }

    /**
     * 稀疏数组转为普通数组
     * @param source
     * @return
     */
    public static Integer[][] toOrdinaryArray(Integer[][] source) {
        //新建普通数组
        Integer[][] ordinaryArray = new Integer[source[0][0]][source[0][1]];
        for (int i = 1; i < source.length; i++) {
            ordinaryArray[source[i][0]][source[i][1]] = source[i][2];
        }
        return ordinaryArray;
    }

    /**
     * 在控制台打印数组
     * @param source
     */
    public static void showArray(Integer[][] source) {
        for (int i = 0; i < source.length; i++) {
            for (int j = 0; j < source[i].length; j++) {
                if (ObjectUtil.isNull(source[i][j])) {
                    System.out.printf("00\t");
                    continue;
                }
                System.out.printf("%d\t", source[i][j]);
            }
            System.out.println();
        }
    }
}

 

标签:count,integers,sparseArray,稀疏,source,数组,Integer
From: https://www.cnblogs.com/xueseng/p/17030166.html

相关文章

  • 数组字符串转数组
    数组字符串转数组Stringstr="[262698,262699,262701]";Integer[]integers=JSON.parseObject(str,Integer[].class); //二维数组字符串转二维数组Stringstr......
  • 2023.1.06 java打印杨辉三角(二维数组)
    publicclassyanghui{publicstaticvoidmain(String[]args){int[][]yanghui=newint[10][];for(inti=0;i<yanghui.length;i++){......
  • 文件和byte数组之间相互转换
    文件转换成byte数组文件转换成byte数组有两种方式:1.传统方式Filefile=newFile("/temp/abc.txt");//initarraywithfilelengthbyte[]bytesArray=newbyte[......
  • Shell 数组
    数组中可以存放多个值。BashShell只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与PHP类似)。与大部分编程语言类似,数组元素的下标由0开始。Shell数组用括......
  • JS数组对象去重同时判断两个属性是否相同
    参考:https://blog.csdn.net/m0_58849641/article/details/124750983?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-1-12......
  • hdu: 张煊的金箍棒(3)(树状数组的区间修改,区间查询)
    ProblemDescription张煊的金箍棒升级了!升级后的金箍棒是由N段相同长度的金属棒连接而成(最开始每段金属棒的价值都是1,从1到N编号);张煊作为金箍棒的主人,可以对金箍棒任意......
  • 【numpy】创建ndarray数组
    创建数组ndarray构造器创建importnumpyasnp'''使用底层ndarray构造器来创建参数说明:名称 描述object 数组或嵌套的数列dtype 数组元素的数据类型,可选cop......
  • 数组
    前言:上篇博客我们学习了函数,紧接着我们趁热打铁,来学习数组,数组在C语言中的地位不输入函数哦1.一维数组的创建和初始化。1.1数组的创建数组是一组相同类型元素的集合。......
  • javascript array 数组 indexOf
    javascriptarray数组indexOf低版本的array是没有indexOf的。参考链接:https://reference.codeproject.com/javascript/Reference/Global_Objects/Array/indexOf/......
  • get请求如何传递数组参数
    问题当我们需要通过get方式传递一个数组作为参数tag:[1,2,3,4]预期是解析为:https://www.cnblogs.com/enter?tag=1&tag=2&tag=3&tag=4然而真相是这样的:https://www.cnb......