-5

给定一个双精度数组

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;

}

这看起来很简单,但我一直无法想出一个令我满意的解决方案,因此提出了这个问题。任何帮助将不胜感激。

4

1 回答 1

2

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

for (double d = Math.floor(start); d <= Math.ceil(end); d += 1.0)
    list.add(d);
于 2012-11-16T01:10:31.297 回答