2016-04-23 102 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

相關問題