欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

面試初級(jí)Java開發(fā)問(wèn)到Arrays

 更新時(shí)間:2021年07月17日 11:37:55   作者:蛋撻學(xué)姐  
這篇文章主要介紹了Java Arrays工具類用法,結(jié)合實(shí)例形式分析了java Arrays工具類針對(duì)數(shù)組元素修改、復(fù)制、排序等操作使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下

一、基本定義

Arrays類,全路徑j(luò)ava.util.Arrays,主要功能為操作數(shù)組,Arrays類的所有方法均為靜態(tài)方法,所以

調(diào)用方式全部為Arrays.方法名

二、常用方法

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

可以將數(shù)組轉(zhuǎn)化為相應(yīng)的list集合,但是也只能轉(zhuǎn)化為list,asList方法內(nèi)部構(gòu)建了一個(gè)內(nèi)部靜態(tài)類ArrayList,

這個(gè)ArrayList也繼承自AbstractList,但并不是我們集合中常用的ArrayList,這兩者是有區(qū)別的,需注意,

內(nèi)部靜態(tài)類AbstractList也實(shí)現(xiàn)了contains,forEach,replaceAll,sort,toArray等方法,但add,remove等方法則沒(méi)有

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開發(fā)交流君樣:593142328一起吹水聊天
List<int[]> list3 = Arrays.asList(array2);

2.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方法有多個(gè)重載,分別對(duì)應(yīng)幾種基本數(shù)據(jù)類型以及引用類型(Object),

fill(int[] a, int val)會(huì)將整個(gè)數(shù)組的值全部覆蓋為val

fill(int[] a, int fromIndex, int toIndex, int val)則提供了可選的開頭和結(jié)尾(不包括)

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");

源碼如下:

我們可以看到可選開頭結(jié)尾的重載方法會(huì)先做數(shù)組越界的校驗(yàn),防止非法輸入

   /** * 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開發(fā)交流君樣: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開發(fā)交流君樣:593142328一起吹水聊天
         throw new ArrayIndexOutOfBoundsException(toIndex);
        }
    }

3.int[] copyOf(int[] original, int newLength)、int[] copyOfRange(int[] original, int from, int to)

存在多個(gè)重載方式,此處以int舉例

從樣例中我i們看到,copyOf復(fù)制后的數(shù)組長(zhǎng)度可以大于復(fù)制前的數(shù)組,根據(jù)源碼發(fā)現(xiàn),超出的元素被填充為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;
    }

對(duì)于copyOfRange,可以選擇復(fù)制的開頭和結(jié)尾(不包括),且結(jié)尾下標(biāo)可以大于原數(shù)組長(zhǎng)度,超出的下標(biāo)會(huì)被填充

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開發(fā)交流君樣: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個(gè)數(shù)組是否相等,基本類型的元素會(huì)依次進(jìn)行==判斷,引用類型則會(huì)在判空后使用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)

假設(shè)我們想輸出一個(gè)數(shù)組的全部元素,一種方法是利用循環(huán)遍歷所有元素后挨個(gè)輸出

但Arrays提供了一個(gè)方案可以直接調(diào)用,toString內(nèi)部實(shí)現(xiàn)其實(shí)也是通過(guò)遍歷來(lái)實(shí)現(xiàn),

利用可變字符串StringBuilder來(lái)構(gòu)建

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(", ");
        }
    }

6.int binarySearch(int[] a, int key)

Arrays內(nèi)置的二分查找方法,使用條件為參數(shù)數(shù)組a是有序的,如無(wú)序

會(huì)導(dǎo)致返回結(jié)果錯(cuò)誤

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     }

總結(jié)

本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • 解決接口調(diào)用報(bào)錯(cuò)newSocketStream(..)failed:Too?many?open?files問(wèn)題

    解決接口調(diào)用報(bào)錯(cuò)newSocketStream(..)failed:Too?many?open?files問(wèn)題

    這篇文章主要介紹了解決接口調(diào)用報(bào)錯(cuò)newSocketStream(..)failed:Too?many?open?files問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 通過(guò)java api實(shí)現(xiàn)解壓縮zip示例

    通過(guò)java api實(shí)現(xiàn)解壓縮zip示例

    這篇文章主要介紹了通過(guò)java api實(shí)現(xiàn)解壓縮zip示例,需要的朋友可以參考下
    2014-04-04
  • 詳解SpringBoot中關(guān)于%2e的Trick

    詳解SpringBoot中關(guān)于%2e的Trick

    這篇文章主要介紹了SpringBoot中關(guān)于%2e的Trick,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • mybatis執(zhí)行update批量更新時(shí)報(bào)錯(cuò)的解決方案

    mybatis執(zhí)行update批量更新時(shí)報(bào)錯(cuò)的解決方案

    這篇文章主要介紹了mybatis執(zhí)行update批量更新時(shí)報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java RMI機(jī)制講解

    Java RMI機(jī)制講解

    這篇文章主要介紹了Java RMI機(jī)制講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼

    mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼

    本文主要介紹了mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 一文秒懂logstash收集springboot日志的方法

    一文秒懂logstash收集springboot日志的方法

    通過(guò)這篇文章帶你了解logstash收集springboot日志的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • Java微信二次開發(fā)(一) Java微信請(qǐng)求驗(yàn)證功能

    Java微信二次開發(fā)(一) Java微信請(qǐng)求驗(yàn)證功能

    這篇文章主要為大家詳細(xì)介紹了Java微信二次開發(fā)第一篇,Java微信請(qǐng)求驗(yàn)證功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • 一文詳解如何使用Java分割PDF文件

    一文詳解如何使用Java分割PDF文件

    PDF是一種用于顯示和打印文檔的文件格式,它非常廣泛地應(yīng)用于電子書籍、報(bào)告、合同等文件的傳遞和共享,這篇文章主要給大家介紹了關(guān)于如何使用Java分割PDF文件的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • 解決JPA @OneToMany及懶加載無(wú)效的問(wèn)題

    解決JPA @OneToMany及懶加載無(wú)效的問(wèn)題

    這篇文章主要介紹了解決JPA @OneToMany及懶加載無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評(píng)論