去除不规则数组中的空行空列

开发中可能会需要去除一些数组中的空行空列,例如以数组格式读取Excel等等操作,这时我们就需要去除一些整的空行空列,避免一些操作出错:

public String[][] removeNullArray(String[][] content){

        //获取空值的列标,及同一列标出现的次数
        //获取空值的行标,及同一行标出现的次数
        Map<Integer,Integer> mapCol = new TreeMap<>();
        Map<Integer,Integer> mapRow = new TreeMap<>();
        //用来记录删除的列数,准确确定要删除的列数,同时在进行空行的删除时,使空行的length准确
        Integer flag = 0;
        for (int i = 0; i < content.length; i++) {
            for (int j = 0; j < content[i].length; j++) {
                if ("".equals(content[i][j])){
                    if (mapCol.get(j) == null){
                        mapCol.put(j, 1);
                    }else {
                        mapCol.put(j, mapCol.get(j) + 1);
                    }

                    if (mapRow.get(i) == null){
                        mapRow.put(i, 1);
                    }else{
                        mapRow.put(i, mapRow.get(i) + 1);
                    }
                }
            }
        }

        //如果同一列标出现的次数等于数组的列数,那么就认为这一列为空值列
        Set<Integer> integers = mapCol.keySet();
        for (Integer integer : integers) {
            if (mapCol.get(integer) == content.length){
                for (int i = 0; i < content.length; i++) {
                    for (int j = integer - flag; j < content[i].length-1; j++) {
                        content[i][j] = content[i][j+1];
                    }
                    content[i] = Arrays.copyOf(content[i],content[i].length-1);

                }
                flag++;
            }
        }

        //如果同一行标出现的次数等于该行标所在行的长度,那么就认为这一行为空值行
        Set<Integer> integersRow = mapRow.keySet();
        int rowIndex = 0;
        for (Integer integer : integersRow) {
            if (mapRow.get(integer) - flag == content[integer - rowIndex].length){
                for (int i = integer - rowIndex; i < content.length - 1; i++) {
                    content[i] = content[i+1];
                }
                content = Arrays.copyOf(content, content.length - 1);
                rowIndex++;
            }
        }

        return content;

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值