java.util.Arrays

备注:本文只对 Java8 中的 java.util.Arrays 中提供的基本功能进行大致介绍,并没有对其具体的实现原理进行深入的探讨和分析。详情可自己深入观摩源码。

本文的结构:

零、开场:Arrays的官方注释

  先来一段官方注释:


  This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.

  Arrays这个类提供了用于操作数组的不同方法(例如:排序和查找),也包含了一个静态工厂,用于允许数组当作链表进行操作。

  The methods in this class all throw a NullPointerException, if the specified array reference is null, except where noted.

  除了有特殊标注的,这个类的所有方法都会在数组引用是 null 时抛出“NullPointerException”空指针异常。


这个注释也是为这个工具类的作用定了基调,其作用基本如下,即注释所体现的:

  1. 用于操作数组的方法:排序、二分查找、比较判断、填充、数组拷贝、数组转链表、数组hashCode、数组转字符串String等
  2. 静态工厂:将数组转化为链表进行操作

一、数组排序:sort

  主要是对基本类型的数组进行排序,排序算法主要是Dual-Pivot Quicksort(双基准快排,暂且未研究,可以百度/谷歌了解下),而非经典的快排(one-pivot单基准)。在可以对原数组元素顺序进行改变时,可以使用这些现成的排序工具方法。

 // 对int型数组进行排序,会改变原数组中的元素位置
public static void sort(int[] a){...}
// 对数组fromIndex下标(包含)到toIndex下标(不包含)的元素进行排序,同样会改变原数组的元素
public static void sort(int[] a, int fromIndex, int toIndex){...}
// long型数组的排序
public static void sort(long[] a){...}
public static void sort(long[] a, int fromIndex, int toIndex){...}
// short型数组的排序
public static void sort(short[] a){...}
public static void sort(short[] a, int fromIndex, int toIndex){...}
// char型数组的排序
public static void sort(char[] a){...}
public static void sort(char[] a, int fromIndex, int toIndex){...}
// byte型数组的排序
public static void sort(byte[] a){...}
public static void sort(byte[] a, int fromIndex, int toIndex){...}
// float型数组的排序
public static void sort(float[] a){...}
public static void sort(float[] a, int fromIndex, int toIndex){...}
// double型数组的排序
public static void sort(double[] a){...}
public static void sort(double[] a, int fromIndex, int toIndex){...}

  还有Object数组和泛型(需要提供Comparator)的比较排序,用的是归并排序。

二、二分法查找数组中的元素:binarySearch

  主要是在一排序的基本类型数组中进行二分查找,找不到指定元素时返回下标值小于0。

 // int排序数组中的二分查找
public static int binarySearch(int[] a, int key){...}
// 对int数组fromIndex下标数组fromIndex下标(包含)到toIndex下标(不包含)的元素的二分查找
public static int binarySearch(int[] a, int fromIndex, int toIndex, int key){...}
// short排序数组中的二分查找
public static int binarySearch(short[] a, short key){...}
public static int binarySearch(short[] a, int fromIndex, int toIndex, short key){...}
// char排序数组中的二分查找
public static int binarySearch(char[] a, char key){...}
public static int binarySearch(char[] a, int fromIndex, int toIndex, char key){...}
// byte排序数组中的二分查找
public static int binarySearch(byte[] a, byte key){...}
public static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key){...}
// double排序数组中的二分查找
public static int binarySearch(double[] a, double key){...}
public static int binarySearch(double[] a, int fromIndex, int toIndex, double key){...}
// float排序数组中的二分查找
public static int binarySearch(float[] a, float key){...}
public static int binarySearch(float[] a, int fromIndex, int toIndex, float key){...}

  还有对Object数组和泛型(需要提供Comparator)的二分查找。

三、比较两个数组是否相等:equals

  判断两个数组是否相等。

 // long数组判断
public static boolean equals(long[] a, long[] a2){...}
// int数组判断
public static boolean equals(int[] a, int[] a2){...}
// short数组判断
public static boolean equals(short[] a, short a2[]){...}
// char数组判断
public static boolean equals(char[] a, char[] a2){...}
// byte数组判断
public static boolean equals(byte[] a, byte[] a2){...}
// boolean数组判断
public static boolean equals(boolean[] a, boolean[] a2){...}
// double数组判断
public static boolean equals(double[] a, double[] a2){...}
// float数组判断
public static boolean equals(float[] a, float[] a2){...}
// Object数组判断,主要使用equals进行判断
public static boolean equals(Object[] a, Object[] a2){...}

四、填数组填充元素:fill,充对数组中的指定位置填充相同的元素内容

  对数组(指定下标或者全部)填充某指定元素

 // long数组
public static void fill(long[] a, long val){...}
public static void fill(long[] a, int fromIndex, int toIndex, long val){...}
// int数组
public static void fill(int[] a, int val){...}
public static void fill(int[] a, int fromIndex, int toIndex, int val){...}
// short数组
public static void fill(short[] a, short val){...}
public static void fill(short[] a, int fromIndex, int toIndex, short val){...}
// char数组
public static void fill(char[] a, char val){...}
public static void fill(char[] a, int fromIndex, int toIndex, char val){...}
// byte数组
public static void fill(byte[] a, byte val){...}
public static void fill(byte[] a, int fromIndex, int toIndex, byte val){...}
// boolean数组
public static void fill(boolean[] a, boolean val){...}
public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val){...}
// double数组
public static void fill(double[] a, double val){...}
public static void fill(double[] a, int fromIndex, int toIndex,double val){...}
// float数组
public static void fill(float[] a, float val){...}
public static void fill(float[] a, int fromIndex, int toIndex, float val){...}
// Object数组
public static void fill(Object[] a, Object val){...}
public static void fill(Object[] a, int fromIndex, int toIndex, Object val){...}

五、数组拷贝:copyOf

  数组的复制:

// 数组的全复制
public static byte[] copyOf(byte[] original, int newLength) {...}
public static short[] copyOf(short[] original, int newLength) {...}
public static int[] copyOf(int[] original, int newLength) {...}
public static long[] copyOf(long[] original, int newLength) {...}
public static char[] copyOf(char[] original, int newLength) {...}
public static float[] copyOf(float[] original, int newLength) {...}
public static double[] copyOf(double[] original, int newLength) {...}
public static boolean[] copyOf(boolean[] original, int newLength) {...}
public static <T> T[] copyOf(T[] original, int newLength) {...}
// 从from下标(包含)到to下标(不包含)的复制版本
public static short[] copyOfRange(xxx[] original, int from, int to) {...}

六、数组转链表:asList,将数组转换为一个固定的List链表对象

  将数组转换成链表形式返回,返回的是内部类ArrayList。对于这个链表,更改操作只有set,而且会改变原来的数组内容;另外就是不支持add操作,对于add调用,会抛出 UnsupportedOperationException 异常(没有重写add实现时的默认实现)。

 public static <T> List<T> asList(T... a){...}

七、计算数组的哈希值:hashCode

  计算数组的hashCode:

public static int hashCode(long a[]) {...}
public static int hashCode(int a[]) {...}
public static int hashCode(short a[]) {...}
public static int hashCode(char a[]) {...}
public static int hashCode(byte a[]) {...}
public static int hashCode(boolean a[]) {...}
public static int hashCode(float a[]) {...}
public static int hashCode(double a[]) {...}
public static int hashCode(Object a[]) {...}

八、数组转String:toString,以特定格式输出数组

  将数组转化为String,格式为:[x, xx, xxx, xxxx],若是数组为空,返回null:

public static String toString(long[] a)
public static String toString(int[] a)
public static String toString(short[] a)
public static String toString(char[] a)
public static String toString(byte[] a)
public static String toString(boolean[] a)
public static String toString(float[] a)
public static String toString(double[] a)
public static String toString(Object[] a)

Java8集合框架——集合工具类Arrays内部方法浅析的更多相关文章

  1. Java:集合框架的工具类

    集合框架的工具类 Arrays:里面都是静态方法,直接用来对各种集合进行操作的公有方法. Collections:里面都是静态方法,直接用来对各种集合进行操作的公有方法. 包括: 1.asList将数 ...

  2. 工具类Arrays.asList()方法把数组转换成集合

    工具类Arrays.asList()方法把数组转换成集合 不能使用其修改集合相关的方法,它的add/remove/clear方法会抛出UnsupportedOperationException() 问 ...

  3. 【JAVA集合框架之工具类】

    一.概述 JAVA集合框架中有两个很重要的工具类,一个是Collections,另一个是Arrays.分别封装了对集合的操作方法和对数组的操作方法,这些操作方法使得程序员的开发更加高效. public ...

  4. 集合框架-工具类-Arrays方法介绍

    1 package cn.itcast.p3.toolclass.arrays.demo; 2 3 import java.util.Arrays; 4 5 public class ArraysDe ...

  5. 集合中的工具类Collections和Arrays

    集合框架的工具类: Collections: 方法sort(): List<String> list = new ArrayList<String>();        lis ...

  6. 12:集合map、工具类

    一.map集合 Map:一次添加一对元素.Collection 一次添加一个元素. Map也称为双列集合,Collection集合称为单列集合. 其实map集合中存储的就是键值对(结婚证书), map ...

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

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

  8. Java最重要的21个技术点和知识点之JAVA集合框架、异常类、IO

    (三)Java最重要的21个技术点和知识点之JAVA集合框架.异常类.IO  写这篇文章的目的是想总结一下自己这么多年JAVA培训的一些心得体会,主要是和一些java基础知识点相关的,所以也希望能分享 ...

  9. java集合框架容器 java框架层级 继承图结构 集合框架的抽象类 集合框架主要实现类

    本文关键词: java集合框架  框架设计理念  容器 继承层级结构 继承图 集合框架中的抽象类  主要的实现类 实现类特性   集合框架分类 集合框架并发包 并发实现类 什么是容器? 由一个或多个确 ...

随机推荐

  1. FORK()函数

    一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同,两个进程也可以做不同的 ...

  2. kali下的截图工具scrot、flameshot和deepin-scrot

    对于这几个截图工具,精简好用的应该是deepin-scrot了,这是个和QQ截图有类似功能的Linux截图工具.flameshot的功能是最多的,也很好用,虽然有的功能用不上. 1.scrot安装和使 ...

  3. Spring加载Properties配置文件的三种方式

    一.通过 context:property-placeholder 标签实现配置文件加载 1) 用法: 1.在spring.xml配置文件中添加标签 <context:property-plac ...

  4. python三大神器===》生成器

    1. 认识生成器 利用迭代器,我们可以在每次迭代获取数据(通过next()方法)时按照特定的规律进行生成.但是我们在实现一个迭代器时,关于当前迭代到的状态需要我们自己记录,进而才能根据当前状态生成下一 ...

  5. python正则表达式匹配多行

    参数re.S jsProp = 'b' fpData = '''var a = []; var b = []; var c = [];'''   .*是尽可能匹配多的 searchResult = r ...

  6. LeetCode874 模拟行走机器人(简单模拟—Java之HashSet简单应用)

    题目: 机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度-1:向右转 90 度1 <= x <= 9 ...

  7. 001、在本地搭建SAP虚拟机环境,用于各种暴力操作

    一.在某网盘下载一个SAP虚拟机,用于SAP学习和相关的测试.打开图中的服务器,点击运行,等灯都变成绿色 二.点击打开熟悉的SAP登录图标 三.很完美的运行起来了. 友情提示:SAP对电脑配置要求挺高 ...

  8. 【SQL必知必会笔记(1)】数据库基础、SQL、MySQL8.0.16下数据库、表的创建及数据插入

    文章目录 1.数据库基础 1.1 数据库(database) 1.2 表(table) 1.3 列和数据类型 1.4 行 1.5 主键 2.什么是SQL 3.创建后续练习所需数据库.表(MySQL8. ...

  9. 配置gem5-gpu模拟环境

    // 系统:ubuntu 14.04,显卡:支持CUDA的显卡.建议换aliyun的源!先clean软件源再update.// 在Ubuntu 16.04上试过,配好环境变量,编译出错(把14.04上 ...

  10. SparkStreaming 笔记

    简介 SparkStreaming是流式处理框架,是Spark API的扩展,支持可扩展.高吞吐量.容错的准实时数据流处理. 实时数据的来源可以是:Kafka, Flume, Twitter, Zer ...