最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年07月17日 11:37:55   作者:蛋撻學(xué)姐  
這篇文章主要介紹了Java Arrays工具類用法,結(jié)合實(shí)例形式分析了java Arrays工具類針對數(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等方法則沒有

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è)重載,分別對應(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ù)組長度可以大于復(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;
    }

對于copyOfRange,可以選擇復(fù)制的開頭和結(jié)尾(不包括),且結(jié)尾下標(biāo)可以大于原數(shù)組長度,超出的下標(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í)也是通過遍歷來實(shí)現(xiàn),

利用可變字符串StringBuilder來構(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是有序的,如無序

會(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é)

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

相關(guān)文章

  • SpringBoot與SpringSecurity整合方法附源碼

    SpringBoot與SpringSecurity整合方法附源碼

    這篇文章主要介紹了SpringBoot與SpringSecurity整合,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • 一文詳細(xì)springboot實(shí)現(xiàn)MySQL數(shù)據(jù)庫的整合步驟

    一文詳細(xì)springboot實(shí)現(xiàn)MySQL數(shù)據(jù)庫的整合步驟

    Spring Boot可以很方便地與MySQL數(shù)據(jù)庫進(jìn)行整合,下面這篇文章主要給大家介紹了關(guān)于springboot實(shí)現(xiàn)MySQL數(shù)據(jù)庫的整合步驟,文中通過圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • 一文搞懂Java SPI機(jī)制的原理與使用

    一文搞懂Java SPI機(jī)制的原理與使用

    Java 程序員在日常工作中經(jīng)常會(huì)聽到 SPI,而且很多框架都使用了 SPI 的技術(shù),那么問題來了,到底什么是 SPI 呢?今天小編就帶大家好好了解一下 SPI
    2022-10-10
  • 最全JVM調(diào)優(yōu)步驟和參數(shù)及配置

    最全JVM調(diào)優(yōu)步驟和參數(shù)及配置

    這篇文章主要給大家介紹了關(guān)于JVM調(diào)優(yōu)的相關(guān)資料,JVM調(diào)優(yōu)是指對Java虛擬機(jī)(JVM)進(jìn)行優(yōu)化,以提高Java程序的性能和運(yùn)行效率,文中介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • Java正則表達(dá)式之全量匹配和部分匹配

    Java正則表達(dá)式之全量匹配和部分匹配

    正則表達(dá)式異常強(qiáng)大,一直理解不深,用的也不深,這次項(xiàng)目中嘗試,體會(huì)到了它的強(qiáng)大之處,這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式之全量匹配和部分匹配的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • Java中FileWriter類的常用方法說明

    Java中FileWriter類的常用方法說明

    這篇文章主要介紹了Java中FileWriter類的常用方法說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java如何自定義線程池中隊(duì)列

    Java如何自定義線程池中隊(duì)列

    這篇文章主要介紹了Java如何自定義線程池中隊(duì)列,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • Java語言實(shí)現(xiàn)簡單FTP軟件 FTP本地文件管理模塊實(shí)現(xiàn)(9)

    Java語言實(shí)現(xiàn)簡單FTP軟件 FTP本地文件管理模塊實(shí)現(xiàn)(9)

    這篇文章主要為大家詳細(xì)介紹了Java語言實(shí)現(xiàn)簡單FTP軟件,F(xiàn)TP本地文件管理模塊的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • JAVA監(jiān)控JMX的使用

    JAVA監(jiān)控JMX的使用

    Java Management Extensions(JMX)提供了一種標(biāo)準(zhǔn)化的方法來管理和監(jiān)控Java應(yīng)用程序,為Java應(yīng)用提供了一種高效、一致的管理方式,本文就來介紹一下JMX的使用,感興趣的可以了解一下
    2024-10-10
  • java獲取系統(tǒng)路徑字體、得到某個(gè)目錄下的所有文件名、獲取當(dāng)前路徑

    java獲取系統(tǒng)路徑字體、得到某個(gè)目錄下的所有文件名、獲取當(dāng)前路徑

    這篇文章主要介紹了java獲取系統(tǒng)路徑字體、得到某個(gè)目錄下的所有文件名、獲取當(dāng)前路徑,需要的朋友可以參考下
    2014-04-04

最新評(píng)論

新疆| 井研县| 长宁县| 宝清县| 舞钢市| 胶南市| 通山县| 中牟县| 四会市| 年辖:市辖区| 徐汇区| 芮城县| 阿合奇县| 蛟河市| 闻喜县| 吴堡县| 红河县| 清流县| 松江区| 伊吾县| 湘潭县| 昔阳县| 永福县| 长丰县| 报价| 得荣县| 林甸县| 北流市| 双江| 壤塘县| 翁牛特旗| 黄平县| 庄河市| 大英县| 延安市| 沂南县| 腾冲县| 滨海县| 安阳市| 桃源县| 慈溪市|