1、Arrays:针对数组进行操作的工具类。比如说排序和查找。
    1:public static String toString(int[] a) 把数组转成字符串
    2:public static void sort(int[] a) 对数组进行排序
    3:public static int binarySearch(int[] a,int key) 二分查找

 import java.util.Arrays; //通过API查找,并不属于long包,所以需要导包
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)); // public static void sort(int[] a) 对数组进行排序
Arrays.sort(arr);
System.out.println("排序后:" + Arrays.toString(arr)); // [13, 24, 57, 69, 80] 为了演示二分查找方法的效果才先对数组进行排序,正常来说不能这样。
// public static int binarySearch(int[] a,int key) 二分查找
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 57));
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));
}
}

2、 Arrays工具类的源码解析
    A、public static String toString(int[] a)
    B、public static void sort(int[] a) 底层是快速排序,知道就可以了。
    C、public static int binarySearch(int[] a,int key)

  开发原则:
    只要是对象,我们就要判断该对象是否为null。

  写的代码:

 int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:" + Arrays.toString(arr)); toString的源码:
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++) { //中间为空,则默认为true,永远进行
b.append(a[i]); //"[24, 69, 80, 57, 13"
if (i == iMax) //限制了循环次数
//"[24, 69, 80, 57, 13]"
return b.append(']').toString();
b.append(", "); //"[24, 69, 80, 57, "
}
}
-----------------------------------------------------
写的代码:
int[] arr = {13, 24, 57, 69, 80};
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577)); binarySearch的源码:
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) {
//无符号右移,相当于(low+hith)/2
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.
}

java 13-2 Arrays工具类的更多相关文章

  1. Java常用API——Arrays工具类

    介绍:Arrays工具类提供了一些可以直接操作数组的方法,以下是一些常用方法: int binarySearch(type[] a, type key):要求数组a元素升序排列,使用二分法搜索key的 ...

  2. Java基础知识强化62:Arrays工具类之概述和使用

    1. Arrays工具类: Arrays这个类包含操作数组(比如排序和查找)的各种方法. 2. Arrays的方法: (1)toString方法:把数组转成字符串 public static Stri ...

  3. Java:集合,Arrays工具类用法

    1. 描述 Arrays工具类提供了针对数组(Array)的一些操作,比如排序.搜索.将数组(Array)转换列表(List)等等,都为静态(static)方法: binarySearch - 使用二 ...

  4. java数据结构1--数组、排序和Arrays工具类

    数组:Array 数组的定义 数组的内存结构 数组定义常见问题 数组常见操作 Java参数传递问题--值传递 二维数组 1.数组概念 同一种类型数据的集合,可以是基本数据类型,也可以是引用数据类型. ...

  5. Java程序员的日常—— Arrays工具类的使用

    这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...

  6. Java基础知识强化之集合框架笔记33:Arrays工具类中asList()方法的使用

    1. Arrays工具类中asList()方法的使用 public static <T> List<T> asList(T... a): 把数组转成集合 注意事项: 虽然可以把 ...

  7. Java基础知识强化63:Arrays工具类之方法源码解析

    1. Arrays工具类的sort方法: public static void sort(int[] a): 底层是快速排序,知道就可以了,用空看. 2. Arrays工具类的toString方法底层 ...

  8. 在Java中Arrays工具类实现功能的六种方法

    使用Arrays工具类,要先导入包即:import.java.util.Arrays 以下是实现六种功能的方法: 1.比较两个数组值是否相等: 结果为true.false.(布尔型不能比较) int ...

  9. JAVA基础——Arrays工具类十大常用方法

    Arrays工具类十大常用方法 原文链接:http://blog.csdn.net/renfufei/article/details/16829457 0. 声明数组 String[] aArray ...

随机推荐

  1. UUID(uuid)js 生成

    全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) . GUID是一种由算法生成的二进制长度 ...

  2. .NET破解之100%营销QQ辅助软件【更新】

    应网友要求,更新一个以前的版本,效果如下: 更改方法 修改一:更改对象的可访问性 Assembly: RWXComLibrary, Version=2.1.0.3 Name: RWXComLibrar ...

  3. Microsoft Dynamics CRM 2013 安装过程 图解

    在安装前,先持一下SQL配置管理,将相关的服务打开.(由于在虚拟机里,许多服务需要时才会打开,像Reporting Services需要处理报表时才打开) 注:Analysis Services 登录 ...

  4. R语言学习笔记:简单的回归分析

    fitbit <- read.csv("fitbit.csv") date     cal   step  dist floor sit inactive walk run2 ...

  5. 利用在线工具自动化生成findviewById

    我们在编码的时候经常会用到findviewById,不厌其烦,我之前介绍过一个很取巧的方法,挺好用的,这里再贴一下: public class KaleBaseActivity extends Act ...

  6. Android 手机号码格式验证

    package com.app.android01 ; import android.app.Activity; import android.os.Bundle; import android.te ...

  7. 安卓开发_浅谈Android动画(一)

    动画效果,针对图片实现 现在学习四种基本的简单动画效果 一.Tween Animation共同属性 1.Duration:动画持续时间(毫秒单位) 2.fillAfter:设置为true,动画转化在动 ...

  8. 【读书笔记】iOS-编码对象

    Cocoa具备一种机制来将对象自身转换为某种格式并保存到磁盘中.对象可以将它们的实例变量和其他数据编码为数据块,然后保存到磁盘中.以后将这些数据块读回到内存中,并且还能基于保存的数据创建新对象.这个过 ...

  9. 异步post请求之代理方法

    #import "ViewController.h" #import "Header.h" @interface ViewController ()<NS ...

  10. Android开发笔记——常见BUG类型之内存泄露与线程安全

    本文内容来源于最近一次内部分享的总结,没来得及详细整理,见谅. 本次分享主要对内存泄露和线程安全这两个问题进行一些说明,内部代码扫描发现的BUG大致分为四类:1)空指针:2)除0:3)内存.资源泄露: ...