2016-04-23 125 views
0

我目前正在编写我的第一个android应用程序,我需要执行excel vlookup的等效操作。我有一个永远不会改变的表格,而且用户不会看到。在这种情况下,应用程序应该使用等于或小于等于的值并返回其等效值(即:7 - > 110.3),否则用户可能会在表格中创建一个值。然后我将使用公式中的返回值。excel vlookup是否有Java的等价物?

. A  B  
1 0 110.3 
2 5 110.3 
3 10 110.7 
4 15 111.2 
5 20 111.3 
6 25 112.3 
+0

在列A中的5各方面因素的价值观? – Jahnold

+0

不,它从0到90的因子是5,但是列A i 91中的最后一个值。 – Novak

回答

1

A TreeMap有找到更高或更低的键和条目的方法。可用于例如这样的:

private static final TreeMap<Integer, Double> table = new TreeMap<Integer, Double>(); 
static { 
    table.put(0, 110.3); 
    table.put(5, 110.3); 
    table.put(10, 110.7); 
    table.put(15, 110.7); 
    table.put(20, 111.2); 
    table.put(25, 112.3); 
} 

private static double lookup(int value) { 
    Entry<Integer, Double> floorEntry = table.floorEntry(value); 
    if (floorEntry == null) 
     return -1; // or throw sth 
    return floorEntry.getValue(); 
} 

public static void main(String[] args) { 
    System.out.println(lookup(7)); 
} 

110.3

相关问题