常见对象-Arrays工具类

本文介绍了Java中使用Arrays类对数组进行的基本操作,包括转换数组为字符串、排序以及二分查找等实用方法,并提供了详细的代码示例。

package cn.itcast_05;

import java.util.Arrays;

/*
 * Arrays:针对数组进行操作的工具类。比如说排序和查找。
 * 1:public static String toString(int[] a) 把数组转成字符串
 * 2:public static void sort(int[] a) 对数组进行排序
 * 3:public static int binarySearch(int[] a,int key) 二分查找
 */
public class ArraysDemo {
	public static void main(String[] args) {
		// 定义一个数组
		int[] arr = { 24, 69, 80, 57, 13 };

		// public static String toString(int[] a) 把数组转成字符串
		System.out.println("排序前:" + Arrays.toString(arr));//排序前:[24, 69, 80, 57, 13]

		// public static void sort(int[] a) 对数组进行排序
		Arrays.sort(arr);
		System.out.println("排序后:" + Arrays.toString(arr));//排序后:[13, 24, 57, 69, 80]

		// [13, 24, 57, 69, 80]
		// public static int binarySearch(int[] a,int key) 二分查找
		System.out.println("binarySearch:" + Arrays.binarySearch(arr, 57)); //binarySearch:2
		System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));//binarySearch:-6
	}
}


public static String toString(int[] a)
public static void sort(int[] a) 底层是快速排序,比较麻烦,知道就可以了。有空看,有问题再问我
public static int binarySearch(int[] a,int key)

开发原则:
	只要是对象,我们就要判断该对象是否为null。否则会出现空指针异常。
	
看源码没有其他技巧,一步一步地看,实在不行,把源码拷贝出去作为你的方法断点

int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:" + Arrays.toString(arr));

public static String toString(int[] a) {
	//a -- arr -- { 24, 69, 80, 57, 13 }

    if (a == null)
        return "null";       //说明数组对象不存在
    int iMax = a.length - 1; //iMax=4;
    if (iMax == -1)
        return "[]";         //说明数组存在,但是没有元素。

    StringBuilder b = new StringBuilder();
    b.append('[');           //"["
    for (int i = 0; ; i++) {
        b.append(a[i]);      //"[24, 69, 80, 57, 13"
        if (i == iMax)     
            return b.append(']').toString(); //"[24, 69, 80, 57, 13]"		
        b.append(", ");      //"[24, 69, 80, 57, "
    }
}
----------------------------------------------------------------------

int[] arr = {13, 24, 57, 69, 80};
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));

public static int binarySearch(int[] a, int key) {
	//a -- arr -- {13, 24, 57, 69, 80}
	//key -- 577
    return binarySearch0(a, 0, a.length, key);
}

private static int binarySearch0(int[] a, int fromIndex, int toIndex, int key) {
    //a -- arr --  {13, 24, 57, 69, 80}
    //fromIndex -- 0
    //toIndex -- 5
    //key -- 577                           
                                 
                                 
    int low = fromIndex;    //low=0
    int high = toIndex - 1; //high=4

    while (low <= high) {
        int mid = (low + high) >>> 1; //mid=2,mid=3,mid=4
        int midVal = a[mid];          //midVal=57,midVal=69,midVal=80

        if (midVal < key)
            low = mid + 1;            //low=3,low=4,low=5
        else if (midVal > key)
            high = mid - 1;
        else
            return mid;               // key found
    }
    return -(low + 1);                // key not found.
	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZHOU_VIP

您的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值