java数组中根据元素查找位置 索引

Arrays提供了一个方便查询的方法 :Arrays.binarySearch();

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] arrays = new String[]{"a","b","c","d","e","fff","g","h","i","j",};
        int positon = Arrays.binarySearch(arrays, "fff");  
         System.out.println("position is:"+positon);
    }

测试结果:

    position is:5  

这个方法也是用的二分法实现的:

    public static int binarySearch(char[] array, int startIndex, int endIndex, char value) {  
            checkBinarySearchBounds(startIndex, endIndex, array.length);
      
            int lo = startIndex;  
            int hi = endIndex - 1;  
      
            while (lo <= hi) {  
                int mid = (lo + hi) >>> 1;//无符号右移  
                char midVal = array[mid];  
      
                if (midVal < value) {  
                    lo = mid + 1;  
                } else if (midVal > value) {  
                    hi = mid - 1;  
                } else {  
                    return mid;  // value found  
                }  
            }  
            return ~lo;  // value not present  
        }

转载:http://blog.csdn.net/mattdong0106/article/details/22676807

Java中可以使用线性查找和二分查找两种方法来查找数组中元素。 线性查找是逐个遍历数组元素,直到找到目标元素或遍历完整个数组。如果找到目标元素,返回该元素数组中索引;如果遍历完整个数组仍未找到目标元素,则返回-1。示例代码如下: ``` public static int getIndex(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i == key) { return i; // 查找到该元素返回该元素数组中索引 } } return -1; // 没有查找到该元素返回-1 } ``` 二分查找是一种更高效的查找算法,但要求数组必须有序。它通过将数组分成两部分,并比较目标元素与中间元素的大小来确定目标元素位置。如果找到目标元素,返回该元素数组中索引;如果未找到目标元素,则返回"-插入点"。插入点指的是在数组中将目标元素插入的位置,即范围内第一个大于目标元素元素索引。示例代码如下: ``` import java.util.Arrays; public class FindArray { public static void main(String[] args) { double[] score = {99.5, 100, 98, 97.5, 110, 95, 85.5, 100}; Arrays.sort(score); // 对数组进行排序 int index1 = Arrays.binarySearch(score, 100); // 查找100的位置 int index2 = Arrays.binarySearch(score, 60); // 查找60的位置 System.out.println("查找到100的位置是:" + index1); System.out.println("查找到60的位置是:" + index2); } } ``` 在以上示例中,我们先使用`Arrays.sort()`方法对数组进行排序,然后使用`Arrays.binarySearch()`方法进行二分查找。当找到目标元素时,返回其在数组中索引;否则返回"-插入点"。 希望以上解答能满足您的需求,如果还有其他问题,请随时提问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [JAVA--数组元素查找方法](https://blog.csdn.net/Xin6Yang/article/details/88778033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Java 数组查找指定元素](https://blog.csdn.net/weixin_52899638/article/details/124508080)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值