JavaSE基础部分--二分查找与Arrays类

二分查找

二分查找思想:
在这里插入图片描述

二分查找前提:
数组必须是有序数组

代码举例:

public class MyTest2 {
    public static void main(String[] args) {
        //查找该元素在数组中第一次出现的索引
        //基本查找:从头开始挨个往找。
        int[] arr = {10, 20, 30, 40, 50, 60, 90};
        //int index = getIndex(arr, 90);
        //二分查找的前提:数组元素有序。
        int index = getIndex2(arr, 40);
        System.out.println(index);

    }
    //二分查找
    private static int getIndex2(int[] arr, int ele) {
        //定义最小索引和最大索引
        int minIndex = 0;
        int maxIndex = arr.length - 1;
        int centerIndex = (minIndex + maxIndex) / 2;
        while (minIndex <= maxIndex) {
            if (ele == arr[centerIndex]) {
                return centerIndex;
            } else if (ele < arr[centerIndex]) {
                maxIndex = centerIndex - 1;
            } else if (ele > arr[centerIndex]) {
                minIndex = centerIndex + 1;
            }
            //记得再次计算中间索引
            centerIndex = (minIndex + maxIndex) / 2;
        }
        return -1;
    }
}

Arrays类

Arrays类是Java提供的对数组进行操作的一个工具类,该工具类中包含许多方法,比如对数字的排序、数组转换字符串、二分查找等

Arrays.toString()方法: 把数组中的元素,转换成一个漂亮字符串。该方法源码如下:

 public static String toString(int[] a) {
     if (a == null)
         return "null";//判断是否为空数组
     int iMax = a.length - 1;
     if (iMax == -1)
         return "[]";
    //遍历拼串
     StringBuilder b = new StringBuilder();
     b.append('[');
     for (int i = 0; ; i++) {
         b.append(a[i]);
         if (i == iMax)
             return b.append(']').toString();
         b.append(", ");
     }   
应用:
public class MyTest {
    public static void main(String[] args) {
        int[] arr={10,20,30};//[10,20,30]
        //toString(arr);把数组中的元素,转换成一个漂亮字符串。
        String s = Arrays.toString(arr);
        System.out.println(s);//[10, 20, 30]
    }
}    

Arrays.sort()方法: 对数组进行排序

Arrays.binarySearch(int[] ,key)方法: 二分查找。该方法源码如下:

private static int binarySearch0(int[] a, int fromIndex, int toIndex,
                                  int key) {
     int low = fromIndex;
     int high = toIndex - 1;
     //采用位运算符的方式计算midVal
     while (<= hilow gh) {
         int mid = (low + high) >>> 1;
         int midVal = a[mid];

         if (midVal < key)
             low = mid + 1;
         else if (midVal > key)
             high = mid - 1;
         else
             return mid; // key found
     }
     return -(low + 1);  // key not found.返回的是  -(低位索引+1)
  }
 应用:
 public class MyTest2 {
    public static void main(String[] args) {
        int[] arr = {20, 20, 6, 8, 9, 1, 2, 3, 4, 90, 100, 3, 0, -1, -2, 6, 9};
        Arrays.sort(arr);//数组排序
        System.out.println(Arrays.toString(arr));
        //二分查找:数组元素必须有序
        int index = Arrays.binarySearch(arr, 1000000);//找不到 返回值是  -(数组长度+1)
        
        System.out.println(index);
        int index1 = Arrays.binarySearch(arr, 100);//返回该数在数组中的索引值
        System.out.println(index1);
        int index2 = Arrays.binarySearch(arr, -3);//找不到 返回的值是 -(最低位索引值-1) 在这个数组中最小的数是-2 它对应的索引值是0 -3小于-2  因此在计算返回值的时候就是-(0+1)
        System.out.println(index2);

    }
}

Arrays.equals()方法: 对两个数组进行比较,判断两个数组是否相等。源代码如下:

//Arrays重写了equals方法
public static boolean equals(int[] a, int[] a2) {
        if (a == a2) {
            return true;
        }

        if (a == null || a2 == null) {
            return false;
        }

        int length = a.length;
        if (a2.length != length) {
            return false;
        }

        for (int i = 0; i < length; i++) {
            if (a[i] != a2[i])
                return false;
        }

        return true;
    }
应用:
public class MyTest3 {
    public static void main(String[] args) {
        int[] arr = {20, 20, 6, 8};
        int[] arr2 = {20, 20, 6,8};
        boolean b = Arrays.equals(arr, arr2);
        System.out.println(b);//true
    }
}

Arrays.copyOf(int[] original, int newLength)方法:
复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组。代码举例如下:

public class MyTest4 {
    public static void main(String[] args) {
      /*  static int[] copyOf ( int[] original, int newLength)复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组
        static int[] copyOfRange ( int[] original, int from, int to)复制旧数组中的指定范围间的几个元素到新数组中*/
        int[] arr = {20, 20, 6, 8,20,30,50,60};
        int[] ints = Arrays.copyOf(arr, 3);

        System.out.println(Arrays.toString(ints));
        //3 起始索引,6 终止索引 含头不含尾
        int[] ints1 = Arrays.copyOfRange(arr, 3, 6);
        System.out.println(Arrays.toString(ints1));

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值