一、基本定义

Arrays类,全路径java.util.Arrays,主要功能为操作数组,Arrays类的所有方法均为静态方法,所以

调用方式全部为Arrays.方法名

二、常用方法

1. <T> List<T>  asList(T... a)

可以将数组转化为相应的list集合,但是也只能转化为list,asList方法内部构建了一个内部静态类ArrayList

这个ArrayList也继承自AbstractList,但并不是我们集合中常用的ArrayList,这两者是有区别的,需注意,

内部静态类AbstractList也实现了contains,forEach,replaceAll,sort,toArray等方法,但add,remove等方法则没有

Integer[] array = new Integer[]{1,2,3};
int[] array2 = new int[]{1,2,3};
List<Integer> list1 = Arrays.asList(1,2,3);
List<Integer> list2 = Arrays.asList(array);
List<int[]> list3 = Arrays.asList(array2);
  1. void fill(int[] a, int val)、void fill(int[] a, int fromIndex, int toIndex, int val)、void fill(Object[] a, Object val)、void fill(Object[] a, int fromIndex, int toIndex, Object val)

fill方法有多个重载,分别对应几种基本数据类型以及引用类型(Object),

fill(int[] a, int val)会将整个数组的值全部覆盖为val
fill(int[] a, int fromIndex, int toIndex, int val)则提供了可选的开头和结尾(不包括)

int[] array = new int[]{1,2,3};
Arrays.fill(array, 1);
Arrays.fill(array, 0, 2, 1);// {1,1,3}
String[] str = {"123"};
Arrays.fill(str, "1");

源码如下:

我们可以看到可选开头结尾的重载方法会先做数组越界的校验,防止非法输入

   /** * Assigns the specified double value to each element of the specified* range of the specified array of doubles.  The range to be filled* extends from index <tt>fromIndex</tt>, inclusive, to index* <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the* range to be filled is empty.)** @param a the array to be filled* @param fromIndex the index of the first element (inclusive) to be*        filled with the specified value* @param toIndex the index of the last element (exclusive) to be*        filled with the specified value* @param val the value to be stored in all elements of the array* @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>* @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or*         <tt>toIndex &gt; a.length</tt> */public static void fill(double[] a, int fromIndex, int toIndex,double val){rangeCheck(a.length, fromIndex, toIndex); for (int i = fromIndex; i < toIndex; i++)a[i] = val;} /** * Assigns the specified float value to each element of the specified array* of floats.** @param a the array to be filled* @param val the value to be stored in all elements of the array */public static void fill(float[] a, float val) {for (int i = 0, len = a.length; i < len; i++)a[i] = val;} /** * Checks that {@code fromIndex} and {@code toIndex} are in* the range and throws an exception if they aren't. */private static void rangeCheck(int arrayLength, int fromIndex, int toIndex) { if (fromIndex > toIndex) { throw new IllegalArgumentException( "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");} if (fromIndex < 0) { throw new ArrayIndexOutOfBoundsException(fromIndex);} if (toIndex > arrayLength) {throw new ArrayIndexOutOfBoundsException(toIndex);}}
  1. int[] copyOf(int[] original, int newLength)、int[] copyOfRange(int[] original, int from, int to)

存在多个重载方式,此处以int举例

从样例中我i们看到,copyOf复制后的数组长度可以大于复制前的数组,根据源码发现,超出的元素被填充为0,引用类型则填充为null

int[] array = new int[]{1,2,3};
int[] array2 = Arrays.copyOf(array, 4);
public static int[] copyOf(
int[] original, int newLength) {
int[] copy = new int[newLength];System.arraycopy(original, 0, copy, 0,Math.min(original.length, newLength));return copy;}

对于copyOfRange,可以选择复制的开头和结尾(不包括),且结尾下标可以大于原数组长度,超出的下标会被填充

int[] array = new int[]{1,2,3,4,5,6,7,8,9};
int[] array2 = Arrays.copyOfRange(array, 3, 6);
int[] array3 = Arrays.copyOfRange(array, 3, 10);
   /** * 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>0</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 zeros 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 int[] copyOfRange(int[] original, int from, int to) { int newLength = to - from; if (newLength < 0) throw new IllegalArgumentException(from + " > " + to); int[] copy = new int[newLength];System.arraycopy(original, from, copy, 0,Math.min(original.length - from, newLength)); return copy;}

4.boolean equals(int[] a, int[] a2)、boolean equals(Object[] a, Object[] a2)

比较2个数组是否相等,基本类型的元素会依次进行==判断,引用类型则会在判空后使用equal

public static boolean equals(int[] a, int[] a2) { if (a==a2) return true; if (a==null || a2==null) return false; int length = a.length; if (a2.length != length) return false; for (int i=0; i<length; i++) if (a[i] != a2[i]) return false; return true;} public static boolean equals(Object[] a, Object[] a2) { if (a==a2) return true; if (a==null || a2==null) return false; int length = a.length; if (a2.length != length) return false; for (int i=0; i<length; i++) {Object o1 = a[i];Object o2 = a2[i]; if (!(o1==null ? o2==null : o1.equals(o2))) return false;} return true;}

5.String toString(int[] a)

假设我们想输出一个数组的全部元素,一种方法是利用循环遍历所有元素后挨个输出

但Arrays提供了一个方案可以直接调用,toString内部实现其实也是通过遍历来实现,

利用可变字符串StringBuilder来构建

public static String toString(int[] a) {
if (a == null) return "null"; int iMax = a.length - 1; if (iMax == -1) return "[]";StringBuilder b = new StringBuilder();b.append('['); for (int i = 0; ; i++) {b.append(a[i]); if (i == iMax) return b.append(']').toString();b.append(", ");}}
  1. int binarySearch(int[] a, int key)

Arrays内置的二分查找方法,使用条件为参数数组a是有序的,如无序

会导致返回结果错误

1  public static int binarySearch(int[] a, int fromIndex, int toIndex,
2                                    int key) {
3         rangeCheck(a.length, fromIndex, toIndex);4         return binarySearch0(a, fromIndex, toIndex, key); 5     }6 7     // Like public version, but without range checks.8     private static int binarySearch0(int[] a, int fromIndex, int toIndex, 9                                      int key) { 10         int low = fromIndex; 11         int high = toIndex - 1; 12
13         while (low <= high) {14             int mid = (low + high) >>> 1; 15             int midVal = a[mid]; 16
17             if (midVal < key)
18                 low = mid + 1;
19             else if (midVal > key)
20                 high = mid - 1;
21             else
22                 return mid; // key found
23 }
24         return -(low + 1);  // key not found.
25     }

最后,祝大家早日学有所成,拿到满意offer

世道变了,面试初级Java开发会问到Arrays!!!你不会还不知道吧!相关推荐

  1. 我丢,去面试初级Java开发岗位,被问到泛型?

    1.泛型的基础概念 1.1 为什么需要泛型 List list = new ArrayList();//默认类型是Objectlist.add("A123");list.add(& ...

  2. 初级Java开发面试必问项!!! 标识符、字面值、变量、数据类型,该学学了!

    最近事情太多,没太时间写博客.今天抽空再整理整理面试中的那点事吧,帮助那些正在找工作或想跳槽找工作的学弟学妹们. 前面我己写过多篇推文,相信看过我文章的伙伴们已经了解掌握了不少.从目前流行的开发技术. ...

  3. 初级Java开发与架构之间的差距不仅仅是开发时间

    转载自  初级Java开发与架构之间的差距不仅仅是开发时间 一.基础篇 JVM JVM内存结构 堆.栈.方法区.直接内存.堆和栈区别 Java内存模型 内存可见性.重排序.顺序一致性.volatile ...

  4. 初级java开发学习路线_成为初级全栈Web开发人员的10分钟路线图

    初级java开发学习路线 So you have started your journey into the world of web development. But what do you lea ...

  5. 面试|应聘Java开发工程师的基本要求是什么?

    根据技术水平不同,Java程序员可以分为初级.中级.高级.资深等.不同级别的Java程序员,企业的要求也是有区别. 下面播妞整理了初级Java程序员和中级Java程序员的应聘要求,供大家参考:(具体要 ...

  6. 初级Java开发工程师!绝密文档,面试手册全面突击!!!秋招已经到来

    这里我要明说一下,不是Java初级和学习Java的千万不要乱看,否则~~~~ 你会怀疑人生,因为会浪费你时间啊!!! 本次考点是Java初级开发工程师面试必备的一些东西!!! 1.数据类型 基本类型 ...

  7. Java岗面试:java开发是什么职业

    前言: 有人说世界上有三个伟大的发明:火,轮子,以及 Kafka. 发展到现在,Apache Kafka 无疑是很成功的,Confluent 公司曾表示世界五百强中有三分之一的企业在使用 Kafka. ...

  8. 恒生java开发复试_2019恒生电子面试经验(JAVA开发人员,实施工程师等)

    为了帮助职业圈网友能够及时了解恒生电子的面试流程以及面试过程所涉及的面试问题,职业圈小编把2019最新恒生电子面试经验编辑好,马上提供给大家,以便能够尽快帮助到有需要的人.文章中还为你提供恒生电子面试 ...

  9. 都说Java行业饱和了,为什么我们公司给初级Java开发开到了12K?

    故事起因: 最近我有个刚毕业的学生问我说:我感觉现在Java行业已经饱和了,也不是说饱和了,是初级的Java根本就没有公司要,哪怕你不要工资也没公司要你,Java刚学出来,没有任何的项目经验和工作经验 ...

最新文章

  1. 解决Java工程URL路径中含有中文的情况
  2. 【转】VC6.0附带小工具软件一览
  3. php swoole 内存,swoole 占用内存到10M 报错
  4. redis基本类型和使用
  5. Java微信公众平台获取签名
  6. 让每一首心动歌曲穿越人海遇见你,背后竟藏着这么多“黑科技”|回响·TME音乐公开课...
  7. 使用Quartus进行功能仿真时出现“testbench_vector_input_file option does not exist”的解决方法
  8. 19.为什么要用异步框架,它解决什么问题?
  9. UILabel实现自适应宽高需要注意的地方(三)
  10. Atitit 常见的bpmn事件类型与触发机制 目录 1. 事件定义概述 2 2. 按照事件的位置分类 2 2.1. 对事件按照位置进行分类,主要可分为开始事件、中间事件和结束事件, 2 3. 按照
  11. Java自学学习路线,自学方法,0基础小白如何怎么样才能用最短的时间学好Java
  12. 十大骨传导耳机品牌,骨传导耳机品牌推荐
  13. html js定义json对象,JS中的JSON对象的定义和取值实现代码
  14. Python 地图行政区边界方案
  15. C站能力认证(C4前端基础认证) //任务二:根据浮动布局以及定位布局的特性,实现构建下列(截图)中的页面
  16. C#打开文件夹加载图片
  17. 系统初始化配置资源失败教程
  18. pyton 内置模块
  19. 数据增强方式mosaic(基于yolo4)代码实现python
  20. c语言编程 遍历字符串,请教大家一个C语言面试的编程题目 C语言:循环执行让用户输入一串字符串,如123456789......

热门文章

  1. 收起.NET程序的dll来
  2. ASP.NET Core中借助CSRedis实现安全高效的分布式锁
  3. 手把手引进门之 ASP.NET Core Entity Framework Core(官方教程翻译版 版本3.2.5)
  4. 下一个计划 : .NET/.NET Core应用性能管理
  5. 活动:北京Xamarin分享会第8期(2017年11月11日)
  6. .Net Core 图片文件上传下载
  7. 来腾讯云开发者实验室 学习.NET
  8. .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类
  9. Vue+Axios同步请求
  10. XunSearch的安装和加入服务器开机脚本以及将目录写入系统变量