开发者社区> 问答> 正文

必须连续的时间的两个点,才能够进行相减:报错

使用java如何计算,如何在某一个时间点断了,需要重新计算距离,必须连续的时间的两个点,才能够进行相减:报错

List<Historylocation> list=Arrays.asList(
                    new Historylocation(12l),
                    new Historylocation(13l),
                    new Historylocation(14l),
                    new Historylocation(15l),
                    new Historylocation(16l),
                    new Historylocation(17l),
                    new Historylocation(18l)
                    );
            
            List<Historylocation> list2=Arrays.asList(
                    new Historylocation(12l,10),
                    new Historylocation(13l,13),
                    new Historylocation(15l,14),
                    new Historylocation(16l,17)
                    );

使用list2对比list进行比,连续才能够相减.如

list 中的12l,13l和list2的12l,13l是相连续的,所以可以相减13-10=3

但是list2中的14l没有,所以需要重新计算从15l开始重新计算,而16L又是连续的所以 

17-14=4;如何算出?

 

 

展开
收起
kun坤 2020-06-14 10:31:47 458 0
1 条回答
写回答
取消 提交回答
  • ```

    HashMap<Long, Integer> longIntegerHashMap = new HashMap<>();
    
    for (int i = 0; i < list2.size(); i++) {
        Historylocation historylocation = list2.get(i);
        longIntegerHashMap.put(historylocation.getKey(), historylocation.getValue());
    }
    
    for (int i = 0; i < list.size(); i++) {
        Integer integer = longIntegerHashMap.get(list.get(i).getKey());
        if (i + 1 >= list.size()) {
            break;
        }
        Integer integer1 = longIntegerHashMap.get(list.get(i + 1).getKey());
    
        if (integer != null && integer1 != null) {
            System.out.println(list.get(i + 1).getKey() + " - " + list.get(i).getKey() + " = " + (integer1 - integer));
        }
    }

    ```

    另一种 设置两个游标, 移动游标位置,进行计算。

    ######17-14=4 ?######

    修正.17-4=3.上述代码已经实现

    ==========================

    2020-06-14 10:31:52
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载