Dust Of Dream

          知識真的是一個圓么?

          ArrayUtils學(xué)習(xí)筆記

          ArrayUtils 擁有以下方法:
          toString
          將一個數(shù)組轉(zhuǎn)換成String,用于打印數(shù)組
          isEquals
          判斷兩個數(shù)組是否相等,采用EqualsBuilder進(jìn)行判斷
          toMap
          將一個數(shù)組轉(zhuǎn)換成Map,如果數(shù)組里是Entry則其Key與Value就是新Map的Key和Value,如果是Object[]則Object[0]為KeyObject[1]為Value
          clone
          拷貝數(shù)組
          subarray
          截取子數(shù)組
          isSameLength
          判斷兩個數(shù)組長度是否相等
          getLength
          獲得數(shù)組的長度
          isSameType
          判段兩個數(shù)組的類型是否相同
          reverse
          數(shù)組反轉(zhuǎn)
          indexOf
          查詢某個Object在數(shù)組中的位置,可以指定起始搜索位置
          lastIndexOf
          反向查詢某個Object在數(shù)組中的位置,可以指定起始搜索位置
          contains
          查詢某個Object是否在數(shù)組中
          toObject
          將基本數(shù)據(jù)類型轉(zhuǎn)換成外包型數(shù)據(jù)
          isEmpty
          判斷數(shù)組是否為空(null和length=0的時(shí)候都為空)
          addAll
          合并兩個數(shù)組
          add
          添加一個數(shù)據(jù)到數(shù)組
          remove
          刪除數(shù)組中某個位置上的數(shù)據(jù)
          removeElement
          刪除數(shù)組中某個對象(從正序開始搜索,刪除第一個)
          eg:
                  // 1.打印數(shù)組
                  ArrayUtils.toString(new int[] { 1423 });// {1,4,2,3}
                  ArrayUtils.toString(new Integer[] { 1423 });// {1,4,2,3}
                  ArrayUtils.toString(null"I'm nothing!");// I'm nothing!

                  
          // 2.判斷兩個數(shù)組是否相等,采用EqualsBuilder進(jìn)行判斷
                  
          // 只有當(dāng)兩個數(shù)組的數(shù)據(jù)類型,長度,數(shù)值順序都相同的時(shí)候,該方法才會返回True
                  
          // 2.1 兩個數(shù)組完全相同
                  ArrayUtils.isEquals(new int[] { 123 }, new int[] { 123 });// true
                  
          // 2.2 數(shù)據(jù)類型以及長度相同,但各個Index上的數(shù)據(jù)不是一一對應(yīng)
                  ArrayUtils.isEquals(new int[] { 132 }, new int[] { 123 });// true
                  
          // 2.3 數(shù)組的長度不一致
                  ArrayUtils.isEquals(new int[] { 1233 }, new int[] { 123 });// false
                  
          // 2.4 不同的數(shù)據(jù)類型
                  ArrayUtils.isEquals(new int[] { 123 }, new long[] { 123 });// false
                  ArrayUtils.isEquals(new Object[] { 123 }, new Object[] { 1, (long23 });// false
                  
          // 2.5 Null處理,如果輸入的兩個數(shù)組都為null時(shí)候則返回true
                  ArrayUtils.isEquals(new int[] { 123 }, null);// false
                  ArrayUtils.isEquals(nullnull);// true

                  
          // 3.將一個數(shù)組轉(zhuǎn)換成Map
                  
          // 如果數(shù)組里是Entry則其Key與Value就是新Map的Key和Value,如果是Object[]則Object[0]為KeyObject[1]為Value
                  
          // 對于Object[]數(shù)組里的元素必須是instanceof Object[]或者Entry,即不支持基本數(shù)據(jù)類型數(shù)組
                  
          // 如:ArrayUtils.toMap(new Object[]{new int[]{1,2},new int[]{3,4}})會出異常
                  ArrayUtils.toMap(new Object[] { new Object[] { 12 }, new Object[] { 34 } });// {1=2,
                  
          // 3=4}
                  ArrayUtils.toMap(new Integer[][] { new Integer[] { 12 }, new Integer[] { 34 } });// {1=2,
                  
          // 3=4}

                  
          // 4.拷貝數(shù)組
                  ArrayUtils.clone(new int[] { 324 });// {3,2,4}

                  
          // 5.截取數(shù)組
                  ArrayUtils.subarray(new int[] { 34156 }, 24);// {1,5}
                  
          // 起始index為2(即第三個數(shù)據(jù))結(jié)束index為4的數(shù)組
                  ArrayUtils.subarray(new int[] { 34156 }, 210);// {1,5,6}
                  
          // 如果endIndex大于數(shù)組的長度,則取beginIndex之后的所有數(shù)據(jù)

                  
          // 6.判斷兩個數(shù)組的長度是否相等
                  ArrayUtils.isSameLength(new Integer[] { 135 }, new Long[] { 2L8L10L });// true

                  
          // 7.獲得數(shù)組的長度
                  ArrayUtils.getLength(new long[] { 1233 });// 3

                  
          // 8.判段兩個數(shù)組的類型是否相同
                  ArrayUtils.isSameType(new long[] { 13 }, new long[] { 856 });// true
                  ArrayUtils.isSameType(new int[] { 13 }, new long[] { 856 });// false

                  
          // 9.數(shù)組反轉(zhuǎn)
                  int[] array = new int[] { 125 };
                  ArrayUtils.reverse(array);
          // {5,2,1}

                  
          // 10.查詢某個Object在數(shù)組中的位置,可以指定起始搜索位置,找不到返回-1
                  
          // 10.1 從正序開始搜索,搜到就返回當(dāng)前的index否則返回-1
                  ArrayUtils.indexOf(new int[] { 136 }, 6);// 2
                  ArrayUtils.indexOf(new int[] { 136 }, 2);// -1
                  
          // 10.2 從逆序開始搜索,搜到就返回當(dāng)前的index否則返回-1
                  ArrayUtils.lastIndexOf(new int[] { 136 }, 6);// 2

                  
          // 11.查詢某個Object是否在數(shù)組中
                  ArrayUtils.contains(new int[] { 312 }, 1);// true
                  
          // 對于Object數(shù)據(jù)是調(diào)用該Object.equals方法進(jìn)行判斷
                  ArrayUtils.contains(new Object[] { 312 }, 1L);// false

                  
          // 12.基本數(shù)據(jù)類型數(shù)組與外包型數(shù)據(jù)類型數(shù)組互轉(zhuǎn)
                  ArrayUtils.toObject(new int[] { 12 });// new Integer[]{Integer,Integer}
                  ArrayUtils.toPrimitive(new Integer[] { new Integer(1), new Integer(2) });// new int[]{1,2}

                  
          // 13.判斷數(shù)組是否為空(null和length=0的時(shí)候都為空)
                  ArrayUtils.isEmpty(new int[0]);// true
                  ArrayUtils.isEmpty(new Object[] { null });// false

                  
          // 14.合并兩個數(shù)組
                  ArrayUtils.addAll(new int[] { 135 }, new int[] { 24 });// {1,3,5,2,4}

                  
          // 15.添加一個數(shù)據(jù)到數(shù)組
                  ArrayUtils.add(new int[] { 135 }, 4);// {1,3,5,4}

                  
          // 16.刪除數(shù)組中某個位置上的數(shù)據(jù)
                  ArrayUtils.remove(new int[] { 135 }, 1);// {1,5}

                  
          // 17.刪除數(shù)組中某個對象(從正序開始搜索,刪除第一個)
                  ArrayUtils.removeElement(new int[] { 135 }, 3);// {1,5}

          posted on 2008-02-26 18:23 Anemone 閱讀(11086) 評論(6)  編輯  收藏 所屬分類: 牧羊心得

          Feedback

          # re: ArrayUtils學(xué)習(xí)筆記[未登錄] 2008-02-26 20:00 eric

          pls check out java.util.collection.Arrays  回復(fù)  更多評論   

          # re: ArrayUtils學(xué)習(xí)筆記 2008-02-26 22:36 ZelluX

          其實(shí)覺得這樣學(xué)效果不好,看了就忘。  回復(fù)  更多評論   

          # re: ArrayUtils學(xué)習(xí)筆記 2008-02-27 10:20 Anemone

          @ZelluX
          我一般習(xí)慣于用代碼記錄心得,我自己有專門的Google項(xiàng)目,里面有自己工作時(shí)候遇到的難題或者實(shí)現(xiàn)方案的示例代碼,發(fā)到博客里主要是方便搜索^^  回復(fù)  更多評論   

          # re: ArrayUtils學(xué)習(xí)筆記 2008-02-28 15:59 離弦之ray的技術(shù)天空

          clone是深拷貝還是淺拷貝??  回復(fù)  更多評論   

          # re: ArrayUtils學(xué)習(xí)筆記 2009-02-05 09:23 ss

          看了就忘記,最高境界。呵呵。  回復(fù)  更多評論   

          # re: ArrayUtils學(xué)習(xí)筆記[未登錄] 2014-11-20 22:28 xx

          @離弦之ray的技術(shù)天空
          淺,看下源碼就知道了  回復(fù)  更多評論   


          My Links

          Blog Stats

          News

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          新聞檔案

          相冊

          常去網(wǎng)站

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 陕西省| 桂东县| 内乡县| 柘城县| 平顺县| 鹤峰县| 华坪县| 延津县| 樟树市| 嘉祥县| 河南省| 海安县| 石景山区| 图木舒克市| 库伦旗| 天水市| 广饶县| 苏尼特左旗| 无极县| 巫溪县| 大化| 锡林郭勒盟| 镇雄县| 庄浪县| 碌曲县| 隆尧县| 图们市| 平邑县| 孝感市| 盈江县| 潞西市| 聂荣县| 永登县| 康马县| 朝阳县| 沙雅县| 遂宁市| 望江县| 太原市| 临潭县| 宣威市|