JAVA實現EXCEL公式專題(六)——尋找引用函數

來源:互聯網
上載者:User

標籤:java   excel   演算法   函數   資料   

尋找引用函數主要有match、lookup、vlookup、hlookup這4種,這幾個常用的方法也就不解釋啦,直接上乾貨:


/** *  項目名稱: *  檔案說明: *  主要特點: EXCEL公式類型:尋找公式 *  版本號碼:1.0 *  製作人:劉晨曦 *  建立時間:2013-12-3 **/package EXCEL;import java.util.Calendar;import java.util.Date;/** * @author lcx * */public class SearchFunctions {/** * 返回指定數值在指定數組地區中的位置 * @param lookupvalue 需要在資料表(lookup_array)中尋找的值。 * @param array :可能包含有所要尋找數值的連續的儲存格範圍 * @param type 表示查詢的指定方式,用數字-1、0或者1表示。 * @param type 為1時,尋找小於或等於lookup_value的最大數值在lookup_array中的位置,lookup_array必須按升序排列: * 為0時,尋找等於lookup_value的第一個數值,lookup_array按任意順序排列: * 為-1時,尋找大於或等於lookup_value的最小數值在lookup_array中的位置, * lookup_array必須按降序排列。利用MATCH函數尋找功能時, * 當尋找條件存在時,MATCH函數結果為具體位置(數值), * 否則顯示#N/A錯誤。 *  @return */public static int match(int lookupvalue,int[] array,int type){int res=0;if(array==null||array.length==0)throw new IllegalArgumentException("數組為空白");//精確匹配if(type==0){for(int i=0;i<array.length;i++){if(array[i]==lookupvalue)return i+1;}throw new IllegalArgumentException("#N/A");}//順序匹配_正序匹配:在尋找index的同時必須滿足整個數組按照正序排列if(type>0){if(lookupvalue<array[0])throw new IllegalArgumentException("#N/A");for(int i=0;i<array.length-1;i++){if(array[i]>array[i+1])throw new IllegalArgumentException("#N/A");if(lookupvalue<array[i])res=res>0?res:i;//已經找到了res 接下來就不用賦值了}if(lookupvalue>array[array.length-1])res=array.length;}//順序匹配_正序匹配:在尋找index的同時必須滿足整個數組按照逆序排列if(type<0){if(lookupvalue>array[0])throw new IllegalArgumentException("#N/A");for(int i=0;i<array.length-1;i++){if(array[i]<array[i+1])throw new IllegalArgumentException("#N/A");if(lookupvalue>array[i])res=res>0?res:i;}if(lookupvalue<array[array.length-1])res=array.length;}return res;}public static int vlookup(int target,int[][] scan,int col){if(scan.length==0||scan[0].length==0)return -Integer.MAX_VALUE;if(col>scan[0].length)return -Integer.MAX_VALUE;for(int i=0;i<scan.length;i++){if(target==scan[i][0])return scan[i][col-1];}return -Integer.MAX_VALUE;}public static int hlookup(int target,int[][] scan,int row){if(scan.length==0||scan[0].length==0)return -Integer.MAX_VALUE;if(row>scan.length)return -Integer.MAX_VALUE;for(int i=0;i<scan[0].length;i++){if(target==scan[0][i])return scan[row-1][i];}return -Integer.MAX_VALUE;}public static int lookup(int target,int[] array1,int[] array2){if(array1==null||array2==null)throw new IllegalArgumentException("#N/A:數組為空白");if(array1.length!=array2.length)throw new IllegalArgumentException("#N/A:數組長度不匹配");int index=-1;for(int i=0;i<array1.length;i++){if(target==array1[i]){index=i;}}if(index<0)throw new IllegalArgumentException("#N/A:沒有匹配項");return array2[index];}public static void main(String[] args) {/*******************測試尋找引用函數*****************************/int[] a={25,35,45,55,65};int[] b={65,55,45,35,25};System.out.println(match(70,a,1));System.out.println(match(45,a,0));System.out.println(match(60,b,-1));System.out.println(match(55,b,-1));int[][] scan=new int[][]{{7,17,117,1117},{8,18,118,1118},{9,19,119,1119}};System.out.println(vlookup(7,scan,3));System.out.println(hlookup(7,scan,3));System.out.println(lookup(35,a,b));}}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

JAVA實現EXCEL公式專題(六)——尋找引用函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.