有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

双精度数组中的java过滤器元素

给定一个双精度数组

1.0 2.0 3.0 ... 10.0 

输出

filter(3,7)  should produce 3.0,4.0,5.0,6.0,7.0

filter(5,7.5) should produce 5.0,6.0,7.0,8.0

filter(6.6,6.7) should produce 6.0,7.0

基于filter方法

public List<Double> filter(double start, double end){
    List<Double> list = Arrays.asList(1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0);
    List<Double> resultList = new ArrayList<Double>(); 

     //search in list for elements such that it includes the lower bounds and upper bound of search
     //i.e. 6.6 should also include 6.0 and 7.0 from the array. 


   return resultList;

}

这似乎很简单,但我未能想出一个我满意的解决方案,因此提出了这个问题。任何帮助都将不胜感激


共 (1) 个答案

  1. # 1 楼答案

    for循环解决方案应该是最简单的,因为您可以使用Math.floor函数来获得所需列表的开头:

    for (double d = Math.floor(start); d <= Math.ceil(end); d += 1.0)
        list.add(d);