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

Java架构没有996
• 阅读 1392

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

一、基本定义

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);//加入Java开发交流君样:593142328一起吹水聊天
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//加入Java开发交流君样:593142328一起吹水聊天
     * @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) {//加入Java开发交流君样:593142328一起吹水聊天
         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 *///加入Java开发交流君样:593142328一起吹水聊天
    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个数组是否相等,基本类型的元素会依次进行==判断,引用类型则会在判空后使用equals【白嫖资料】

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!!!你不会还不知道吧!

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
Java日期时间API系列31
  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这