Vincent.Chan‘s Blog

          常用鏈接

          統(tǒng)計(jì)

          積分與排名

          網(wǎng)站

          最新評論

          [Jakarta Commons筆記] 代碼范例 - ArrayUtils

          數(shù)組是我們經(jīng)常需要使用到的一種數(shù)據(jù)結(jié)構(gòu),但是由于Java本身并沒有提供很好的API支持,使得很多操作實(shí)際上做起來相當(dāng)繁瑣,以至于我們實(shí)際編碼中甚至?xí)幌奚阅苋ナ褂?/span>Collections API,用Collection當(dāng)然能夠很方便的解決我們的問題,但是我們一定要以性能為代價(jià)嗎?ArrayUtils幫我們解決了處理類似情況的大部分問題。來看一個(gè)例子:

           

          package sean.study.jakarta.commons.lang;

           

          import java.util.Map;

          import org.apache.commons.lang.ArrayUtils;

           

          public class ArrayUtilsUsage {

           

              public static void main(String[] args) {

           

                  // data setup

                  int[] intArray1 = { 2, 4, 8, 16 };

                  int[][] intArray2 = { { 1, 2 }, { 2, 4 }, { 3, 8 }, { 4, 16 } };

                  Object[][] notAMap = {

                          { "A", new Double(100) },

                          { "B", new Double(80) },

                          { "C", new Double(60) },

                          { "D", new Double(40) },

                          { "E", new Double(20) }

                  };

           

                  // printing arrays

                  System.out.println("intArray1: " + ArrayUtils.toString(intArray1));

                  System.out.println("intArray2: " + ArrayUtils.toString(intArray2));

                  System.out.println("notAMap: " + ArrayUtils.toString(notAMap));

           

                  // finding items

                  System.out.println("intArray1 contains '8'? "

                          + ArrayUtils.contains(intArray1, 8));

                  System.out.println("intArray1 index of '8'? "

                          + ArrayUtils.indexOf(intArray1, 8));

                  System.out.println("intArray1 last index of '8'? "

                          + ArrayUtils.lastIndexOf(intArray1, 8));

           

                  // cloning and resversing

                  int[] intArray3 = ArrayUtils.clone(intArray1);

                  System.out.println("intArray3: " + ArrayUtils.toString(intArray3));

                  ArrayUtils.reverse(intArray3);

                  System.out.println("intArray3 reversed: "

                          + ArrayUtils.toString(intArray3));

           

                  // primitive to Object array

                  Integer[] integerArray1 = ArrayUtils.toObject(intArray1);

                  System.out.println("integerArray1: "

                          + ArrayUtils.toString(integerArray1));

           

                  // build Map from two dimensional array

                  Map map = ArrayUtils.toMap(notAMap);

                  Double res = (Double) map.get("C");

                  System.out.println("get 'C' from map: " + res);

           

              }

           

          }

           

          以下是運(yùn)行結(jié)果:

           

          intArray1: {2,4,8,16}

          intArray2: {{1,2},{2,4},{3,8},{4,16}}

          notAMap: {{A,100.0},{B,80.0},{C,60.0},{D,40.0},{E,20.0}}

          intArray1 contains '8'? true

          intArray1 index of '8'? 2

          intArray1 last index of '8'? 2

          intArray3: {2,4,8,16}

          intArray3 reversed: {16,8,4,2}

          integerArray1: {2,4,8,16}

          get 'C' from map: 60.0

           

          這段代碼說明了我們可以如何方便的利用ArrayUtils類幫我們完成數(shù)組的打印、查找、克隆、倒序、以及值型/對象數(shù)組之間的轉(zhuǎn)換等操作。如果想了解更多,請參考Javadoc

          posted on 2006-03-10 23:06 Vincent.Chen 閱讀(138) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 海盐县| 建阳市| 陆河县| 林甸县| 调兵山市| 长寿区| 绍兴县| 兰州市| 镇坪县| 司法| 常州市| 通州市| 依安县| 梅州市| 河南省| 珠海市| 安岳县| 洛宁县| 昌邑市| 伊金霍洛旗| 宝丰县| 邯郸县| 邯郸市| 渝中区| 筠连县| 静安区| 南和县| 安化县| 辉南县| 碌曲县| 渑池县| 礼泉县| 上栗县| 会东县| 固原市| 新泰市| 荥阳市| 永丰县| 大冶市| 兴业县| 汉中市|