博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java-工具类Collections和Arrays的设计和区别
阅读量:6005 次
发布时间:2019-06-20

本文共 3062 字,大约阅读时间需要 10 分钟。

Arrays

1.作用

看类的名字,就知道是对数组(数据类型[])进行各种操作。例如,排序、查找、复制等。

排序的算法是归并排序。

查找的算法是二分查找。
复制是调用System.arraysCopy()。

2.官方API

public class Arrays
extends Object
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.
The methods in this class all throw a NullPointerException, if the specified array reference is null, except where noted.
这个类包含各种操作数组的方法,例如,排序、查找等。这个类也包含了一个静态工厂方法,允许数组被作为集合(list)查看。
如果指定的数组引用是null,那么方法会抛出NullPointerException。

The documentation for the methods contained in this class includes briefs description of the implementations. Such descriptions should be regarded as implementation notes, rather than parts of the specification. Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to. (For example, the algorithm used by sort(Object[]) does not have to be a MergeSort, but it does have to be stable.)

方法的文档包含了实现的简单介绍,但是不属于规范的一部分。你可以用别的算法代替Arrays的算法,只要遵守规范即可。
举个例子,sort()的算法可以不是使用归并排序,但是该算法一定要是稳定的。

This class is a member of the Java Collections Framework.

这个类是Java Collections Framework的一部分。

Collections

1.作用

看类的名字,就知道是对集合类(Collection的子类)进行操作。例如,排序等。

排序的算法也是合并排序,调用的是Arrays.sort()。

2.官方API

public class Collections
extends Object
This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends.
这个类只包含操作或返回集合的静态方法。它包含了多种算法...。

The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.

如果方法的参数是null,那么会抛出NullPointerException 。

The documentation for the polymorphic algorithms contained in this class generally includes a brief description of the implementation. Such descriptions should be regarded as implementation notes, rather than parts of the specification. Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to. (For example, the algorithm used by sort does not have to be a mergesort, but it does have to be stable.)

同上。

The "destructive" algorithms contained in this class, that is, the algorithms that modify the collection on which they operate, are specified to throw UnsupportedOperationException if the collection does not support the appropriate mutation primitive(s), such as the set method. These algorithms may, but are not required to, throw this exception if an invocation would have no effect on the collection. For example, invoking the sort method on an unmodifiable list that is already sorted may or may not throw UnsupportedOperationException.

This class is a member of the Java Collections Framework.

排序

1.Arrays

Arrays.sort()
2.Collections
Collections.sort()。
调用的也是Arrays.sort()。具体来说是,第一步,先把集合转换为数组,第二步,调用Arrays.sort()。

复制对象

1.Arrays

Arrays.copyOf()。
调用的是System.arrayCopy()。

Arrays.copyOf()和System.arrayCopy()没有什么区别,只是传参有点不同。

查找

转载地址:http://wspmx.baihongyu.com/

你可能感兴趣的文章
提供SaaS Launchkit,快速定制,一云多端等能力,一云多端将通过小程序云实现...
查看>>
java b2b2c SpringCloud电子商务平台
查看>>
(十三)企业分布式微服务云SpringCloud SpringBoot mybatis-断路器聚合监控(Hystrix Turbine)...
查看>>
热更新
查看>>
亿万富翁Calvin Ayre梭哈BCH!
查看>>
map/reduce之间的shuffle,partition,combiner过程的详解
查看>>
ubuntu 手动配置interface上网
查看>>
Notes 迁移到SharePoint
查看>>
CentOS6.3修改yum源用本地系统光盘来安装gcc
查看>>
awk
查看>>
从零开始学Python-day5
查看>>
009Linux管理日常使用的基本命令
查看>>
MAC 查看java home目录
查看>>
两数之和(输入为二叉树) Two Sum IV - Input is a BST
查看>>
我的友情链接
查看>>
Java 常见异常类
查看>>
红帆移动OA for iPhone更新v1.0.2
查看>>
CentOS 6.5下编译安装httpd+mysql+php+phpMyAdmin
查看>>
Eclipse修改字体大小
查看>>
Cacti三大模板关系图
查看>>