搜索在数组与Java自定义比较 [英] Searching in an array with custom comparator in java

查看:139
本文介绍了搜索在数组与Java自定义比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么总是返回 49999 不管是什么 strToSearch 变量保存?即使有铮铮搜索变量返回相同。我错过了一些东西:

Why does it always return 49999 no matter what strToSearch variable holds? Even with the clank search variable it returns the same. Have I missed something:

    String[] arr = new String[100000];
    String strToSearch = "12";
    for (int i = 0; i < arr.length; i++) {
        arr[i] = i+","+i;
    }
    Arrays.sort(arr, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            if(o1 != null && o2 != null && !o1.isEmpty() && !o2.isEmpty() && o1.indexOf(",") != -1 && o2.indexOf(",") != -1) {
                String[] o1Arr = o1.split(",");
                String[] o2Arr = o2.split(",");
                Integer one = Integer.parseInt(o1Arr[0]);
                Integer two = Integer.parseInt(o2Arr[0]);
                return one.compareTo(two);
            }
            return 0;
        }
    });
    System.out.println(Arrays.binarySearch(arr, strToSearch, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            if(o1 != null && o2 != null && !o1.isEmpty() && !o2.isEmpty() && o1.indexOf(",") != -1 && o2.indexOf(",") != -1) {
                String[] o1Arr = o1.split(",");
                String[] o2Arr = o2.split(",");
                return o1Arr[0].compareTo(o2Arr[0]);
            }
            return 0;
        }
    }));

我的数组包含逗号分隔值,我想根据数组元素的逗号之前的字符串进行搜索。是否有任何其他简单的解决方案可用?我也取得了哪些循环结束阵列,并认为,但我正在寻找一些替代字符串的自定义方法。

My array contains comma separated values and I want to search based on string before comma in array elements. Is there any other simple solution available? I have also made a custom method which loops over array and finds the string but I'm looking for some alternatives.

推荐答案

Arrays.binarySearch(...) Javadoc中说明该数组必须已经进行分类和因此,比较数组值和搜索字符串,并没有实际比较IS的的再次用于排序数组。

The JavaDoc on Arrays.binarySearch(...) states that the array must already be sorted and thus the comparator actually compares the array value and the search string and not is not used for sorting the array again.

这意味着你得到的东西像比较(ARR [X],12)键,您如果这两个字符串必须包含一个逗号条件状态,否则他们是平等的。和12不包含任何逗号,从而使12等于的每一个阵列(总是返回0),在元素

This means you get something like compare(arr[x], "12") and your if condition states that both strings must contain a comma, otherwise they are equal. And "12" doesn't contain any commas, thus making "12" equal to every element in the array (always returns 0).

修改的:

查看源$ C ​​$ C似乎支持我的假设。
有这条线,例如:

Looking at the source code seems to support my assumption. There's this line, for example:

 //c is the Comparator
 //midVal is an element of your array 
 //key is the key parameter you passed to binarySearch(...)
 c.compare(midVal, key); 

之所以49999返回的是,第一个中期值是指数中期=(低+高)GT;&GT; 1 (0 + 99999)GT;&GT; 1 == 49999 和由于比较返回0值被视为相等。只听轰的一声,该值被发现。

The reason for 49999 being returned is that the first mid value is at index mid = (low + high) >> 1 which is (0 + 99999) >> 1 == 49999 and since the comparator returns 0 the values are considered equal. And boom, the value is "found".

这篇关于搜索在数组与Java自定义比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆