Dust Of Dream

          知識真的是一個圓么?

          ArrayUtils學習筆記

          ArrayUtils 擁有以下方法:
          toString
          將一個數組轉換成String,用于打印數組
          isEquals
          判斷兩個數組是否相等,采用EqualsBuilder進行判斷
          toMap
          將一個數組轉換成Map,如果數組里是Entry則其Key與Value就是新Map的Key和Value,如果是Object[]則Object[0]為KeyObject[1]為Value
          clone
          拷貝數組
          subarray
          截取子數組
          isSameLength
          判斷兩個數組長度是否相等
          getLength
          獲得數組的長度
          isSameType
          判段兩個數組的類型是否相同
          reverse
          數組反轉
          indexOf
          查詢某個Object在數組中的位置,可以指定起始搜索位置
          lastIndexOf
          反向查詢某個Object在數組中的位置,可以指定起始搜索位置
          contains
          查詢某個Object是否在數組中
          toObject
          將基本數據類型轉換成外包型數據
          isEmpty
          判斷數組是否為空(null和length=0的時候都為空)
          addAll
          合并兩個數組
          add
          添加一個數據到數組
          remove
          刪除數組中某個位置上的數據
          removeElement
          刪除數組中某個對象(從正序開始搜索,刪除第一個)
          eg:
                  // 1.打印數組
                  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.判斷兩個數組是否相等,采用EqualsBuilder進行判斷
                  
          // 只有當兩個數組的數據類型,長度,數值順序都相同的時候,該方法才會返回True
                  
          // 2.1 兩個數組完全相同
                  ArrayUtils.isEquals(new int[] { 123 }, new int[] { 123 });// true
                  
          // 2.2 數據類型以及長度相同,但各個Index上的數據不是一一對應
                  ArrayUtils.isEquals(new int[] { 132 }, new int[] { 123 });// true
                  
          // 2.3 數組的長度不一致
                  ArrayUtils.isEquals(new int[] { 1233 }, new int[] { 123 });// false
                  
          // 2.4 不同的數據類型
                  ArrayUtils.isEquals(new int[] { 123 }, new long[] { 123 });// false
                  ArrayUtils.isEquals(new Object[] { 123 }, new Object[] { 1, (long23 });// false
                  
          // 2.5 Null處理,如果輸入的兩個數組都為null時候則返回true
                  ArrayUtils.isEquals(new int[] { 123 }, null);// false
                  ArrayUtils.isEquals(nullnull);// true

                  
          // 3.將一個數組轉換成Map
                  
          // 如果數組里是Entry則其Key與Value就是新Map的Key和Value,如果是Object[]則Object[0]為KeyObject[1]為Value
                  
          // 對于Object[]數組里的元素必須是instanceof Object[]或者Entry,即不支持基本數據類型數組
                  
          // 如: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.拷貝數組
                  ArrayUtils.clone(new int[] { 324 });// {3,2,4}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

          Feedback

          # re: ArrayUtils學習筆記[未登錄] 2008-02-26 20:00 eric

          pls check out java.util.collection.Arrays  回復  更多評論   

          # re: ArrayUtils學習筆記 2008-02-26 22:36 ZelluX

          其實覺得這樣學效果不好,看了就忘。  回復  更多評論   

          # re: ArrayUtils學習筆記 2008-02-27 10:20 Anemone

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

          # re: ArrayUtils學習筆記 2008-02-28 15:59 離弦之ray的技術天空

          clone是深拷貝還是淺拷貝??  回復  更多評論   

          # re: ArrayUtils學習筆記 2009-02-05 09:23 ss

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

          # re: ArrayUtils學習筆記[未登錄] 2014-11-20 22:28 xx

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


          My Links

          Blog Stats

          News

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          新聞檔案

          相冊

          常去網站

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 略阳县| 上林县| 曲靖市| 阜康市| 连平县| 施甸县| 梨树县| 香港| 盐池县| 顺平县| 饶河县| 濮阳市| 长汀县| 房山区| 福海县| 柳州市| 清流县| 宿松县| 陈巴尔虎旗| 雷山县| 泗水县| 保亭| 西平县| 太白县| 濉溪县| 海原县| 海宁市| 通渭县| 宁城县| 广昌县| 乐业县| 武定县| 滦南县| 塔河县| 孟州市| 泰安市| 建德市| 黄梅县| 郴州市| 大理市| 阳朔县|