一、常用使用方法

1.1 获取一个int数组的最大值和最小值

return Arrays.stream(arr).max().getAsInt();
return Arrays.stream(arr).min().getAsInt();

1.2 获取一个int数组的和

return Arrays.stream(arr).sum();

Java中求数组的和及平均数

二、方法

2.1 copyof

/*** Copies the specified array, truncating or padding with null characters (if necessary)* so the copy has the specified length.  For all indices that are valid* in both the original array and the copy, the two arrays will contain* identical values.  For any indices that are valid in the copy but not* the original, the copy will contain <tt>'\\u000'</tt>.  Such indices* will exist if and only if the specified length is greater than that of* the original array.** @param original the array to be copied* @param newLength the length of the copy to be returned* @return a copy of the original array, truncated or padded with null characters*     to obtain the specified length* @throws NegativeArraySizeException if <tt>newLength</tt> is negative* @throws NullPointerException if <tt>original</tt> is null* @since 1.6*/public static char[] copyOf(char[] original, int newLength) {char[] copy = new char[newLength];System.arraycopy(original, 0, copy, 0,Math.min(original.length, newLength));return copy;}

2.2 copyOfRange

/*** Copies the specified range of the specified array into a new array.* The initial index of the range (<tt>from</tt>) must lie between zero* and <tt>original.length</tt>, inclusive.  The value at* <tt>original[from]</tt> is placed into the initial element of the copy* (unless <tt>from == original.length</tt> or <tt>from == to</tt>).* Values from subsequent elements in the original array are placed into* subsequent elements in the copy.  The final index of the range* (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,* may be greater than <tt>original.length</tt>, in which case* <tt>'\\u000'</tt> is placed in all elements of the copy whose index is* greater than or equal to <tt>original.length - from</tt>.  The length* of the returned array will be <tt>to - from</tt>.** @param original the array from which a range is to be copied* @param from the initial index of the range to be copied, inclusive* @param to the final index of the range to be copied, exclusive.*     (This index may lie outside the array.)* @return a new array containing the specified range from the original array,*     truncated or padded with null characters to obtain the required length* @throws ArrayIndexOutOfBoundsException if {@code from < 0}*     or {@code from > original.length}* @throws IllegalArgumentException if <tt>from &gt; to</tt>* @throws NullPointerException if <tt>original</tt> is null* @since 1.6*/public static char[] copyOfRange(char[] original, int from, int to) {int newLength = to - from;if (newLength < 0)throw new IllegalArgumentException(from + " > " + to);char[] copy = new char[newLength];System.arraycopy(original, from, copy, 0,Math.min(original.length - from, newLength));return copy;}

2.3 asList

/*** Returns a fixed-size list backed by the specified array.  (Changes to* the returned list "write through" to the array.)  This method acts* as bridge between array-based and collection-based APIs, in* combination with {@link Collection#toArray}.  The returned list is* serializable and implements {@link RandomAccess}.** <p>This method also provides a convenient way to create a fixed-size* list initialized to contain several elements:* <pre>*     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");* </pre>** @param <T> the class of the objects in the array* @param a the array by which the list will be backed* @return a list view of the specified array*/@SafeVarargs@SuppressWarnings("varargs")public static <T> List<T> asList(T... a) {return new ArrayList<>(a);}

2.4 sort

根据传入的comparator来排序传入的object数组,这个方法提供稳定排序。

算法的实现是一个特殊的(stable、adaptive、iterative)归并排序,如果传入的数组已经部分有序了,那么只需要远少于n lg(n)次的比较,如果数组完全乱序,那么算法的表现类似于普通的归并排序。如果数组已经几乎是排序状态,那么算法只需要大约n次比较。如果数组基本有序,那么只需要常数级别的额外空间,如果数组无序,那么需要额外的n/2引用空间。

/*** <p>The implementation takes equal advantage of ascending and* descending order in its input array, and can take advantage of* ascending and descending order in different parts of the the same* input array.  It is well-suited to merging two or more sorted arrays:* simply concatenate the arrays and sort the resulting array.** <p>The implementation was adapted from Tim Peters's list sort for Python* (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">* TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic* Sorting and Information Theoretic Complexity", in Proceedings of the* Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,* January 1993.** @param <T> the class of the objects to be sorted* @param a the array to be sorted* @param c the comparator to determine the order of the array.  A*        {@code null} value indicates that the elements'*        {@linkplain Comparable natural ordering} should be used.* @throws ClassCastException if the array contains elements that are*         not <i>mutually comparable</i> using the specified comparator* @throws IllegalArgumentException (optional) if the comparator is*         found to violate the {@link Comparator} contract*/public static <T> void sort(T[] a, Comparator<? super T> c) {if (c == null) {sort(a);} else {if (LegacyMergeSort.userRequested)legacyMergeSort(a, c);elseTimSort.sort(a, 0, a.length, c, null, 0, 0);}}

jdk Arrays类相关推荐

  1. Java基础学习笔记之:System类;Math类;Arrays类BigInteger,BigDecimal

    System类 在API中System类介绍的比较简单,我们给出定义,System中代表程序所在系统,提供了对应的一些系统属性信息,和系统操作. System类不能手动创建对象,因为构造方法被priv ...

  2. Java——Arrays类操作数组的工具类

    JDK中提供了一个专门用于操作数组的工具类,即 Arrays 类,位于 Java.util 包中.该类提供了一系列方法来操作数组,如排序.复制.比较.填充等,用户直接调用这些方法即可,不需要自己编码实 ...

  3. Apache Commons ArrayUtils.toString(Object)与JDK Arrays.toString(Object)

    Apache Commons Lang提供了一个ArrayUtils类,其中包含toString(Object)方法,该方法"将数组作为字符串输出". 在本文中,我将研究当JDK提 ...

  4. Arrays类及其方法分析

    排序 Arrays.sort()方法,对于基本数据类型采用DualPivotQuicksort(多路快排)进行排序,对于引用类型的数组,采用MergeSort(归并排序)进行排序,下面我们分别来讲一下 ...

  5. 【JAVA SE】第五章 数组、多维数组和Arrays类

    第五章 数组.多维数组和Arrays类 文章目录 第五章 数组.多维数组和Arrays类 一.数组 1.概述 2.声明数组变量 3.创建数组 4.For-Each 循环 二.多维数组 1.概述 2.多 ...

  6. java.util 1.8_JDK1.8源码(四)——java.util.Arrays 类

    java.util.Arrays 类是 JDK 提供的一个工具类,用来处理数组的各种方法,而且每个方法基本上都是静态方法,能直接通过类名Arrays调用. 1.asList public static ...

  7. java的数组与Arrays类源码详解

    java的数组与Arrays类源码详解 java.util.Arrays 类是 JDK 提供的一个工具类,用来处理数组的各种方法,而且每个方法基本上都是静态方法,能直接通过类名Arrays调用. 类的 ...

  8. Java数组02:数组的使用,多维数组,Arrays类,冒泡排序和稀疏数组

    1.数组的使用 1.1 普通for循环 package com.tianyu.array;public class ArrayDemo03 {public static void main(Strin ...

  9. Java基础——Arrays类

    每日正能量 如果你要做一件事,不要到处宣言自己的想法,只管安安静静地去做,值不值,时间是最好的证明,自己的人生,得自己负责. Arrays类 JDK中提供了一个专门用于操作数组的工具类,即Arrays ...

  10. Arrays类讲解:

    1. 数组的工具类:java.uti.Arrays 2.由于数组对象本身并没有方法可以供我们调用,但API中提供了一个工具类Arrays供我们使用,从而可以对数据对象进行一些基本的操作. 3.查看JD ...

最新文章

  1. linux 内存越界判断_LINUX 共享内存越界
  2. python实战-pdf文件转txt
  3. vue element-ui 的奇怪组件el-switch
  4. android获取系统当前年月日时分秒的时间
  5. [转]5个JavaScript面试题
  6. Note that ‘/home/w/.local/share‘ is not in the search pathset by the XDG_DATA_HOME and XDG_DATA_DIRS
  7. 安卓 电话黑名单拦截
  8. QCC3005 控制AMP_Mute的管脚配置问题
  9. 计算机图形学期末考试题及答案,计算机图形学期末考试卷答案
  10. 钉钉扫码登录二维码错乱
  11. 应用交付能给客户带来什么价值?
  12. CE自动汇编之AOB注入
  13. 永安行专利侵权胜诉,IPO并未搁置
  14. 关于BatchNorm的理解与讨论
  15. python集合和列表、安从小到大的顺序组合成一个字典_第3章 Python容器:列表、元组、字典与集合...
  16. java bbs论坛管理系统_BBS论坛管理系统
  17. 游戏模型外包-【精刚石数位】
  18. java 垃圾收集器_JVM垃圾收集器详解
  19. 官方代付系统/支付宝微信代付/企业付款/提现秒到
  20. 英语学习-索贝斯演讲学习摘要

热门文章

  1. c语言发票的编码,关于增值税发票中商品税收分类编码对应的简称
  2. 图神经网络总结(GCN/GAT/GraphSAGE/DeepWalk/TransE)
  3. AAAI论文Joint Extraction of Entities and Overlapping Relations Using Position-Attentive Sequence阅读笔记
  4. C++基础语法-01-引用
  5. 幅频响应 matlab画法,MATLAB环境下频率响应曲线的绘制方法.pdf
  6. python实现取出一个列表或者多个列表中的公共前缀
  7. nodejs如何实现ajax,nodejs + express怎么实现Ajax方式及其简单功能
  8. 工程力学考研 可以转计算机专业吗,跨专业考研我是工程力学的本科生,想要考飞行 – 手机爱问...
  9. java对公项目_5个让人激动的Java项目
  10. mysql 5.7 root 修改密码_Mysql5.7忘记root密码及mysql5.7修改root密码的方法