qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請?jiān)L問 http://qaseven.github.io/

          關(guān)于Java數(shù)組的12個(gè)最佳方法

            下面是stackoverflow中關(guān)于數(shù)組方法的相關(guān)問題中,獲得最多票數(shù)的12個(gè)數(shù)組操作方法。
            1.  聲明一個(gè)數(shù)組
            String[] aArray = new String[5];
            String[] bArray = {"a","b","c", "d", "e"};
            String[] cArray = new String[]{"a","b","c","d","e"};
            2.  輸出一個(gè)數(shù)組
            int[] intArray = { 1, 2, 3, 4, 5 };
            String intArrayString = Arrays.toString(intArray);
            // print directly will print reference value
            System.out.println(intArray);
            // [I@7150bd4d
            System.out.println(intArrayString);
            // [1, 2, 3, 4, 5]
            3.  從一個(gè)數(shù)組創(chuàng)建數(shù)組列表
            String[] stringArray = { "a", "b", "c", "d", "e" };
            ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
            System.out.println(arrayList);
            // [a, b, c, d, e]
            4.  檢查一個(gè)數(shù)組是否包含某個(gè)值
            String[] stringArray = { "a", "b", "c", "d", "e" };
            boolean b = Arrays.asList(stringArray).contains("a");
            System.out.println(b);
            // true
            5.  連接兩個(gè)數(shù)組
            int[] intArray = { 1, 2, 3, 4, 5 };
            int[] intArray2 = { 6, 7, 8, 9, 10 };
            // Apache Commons Lang library
            int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
            6.  聲明一個(gè)內(nèi)聯(lián)數(shù)組(Array inline)
            method(new String[]{"a", "b", "c", "d", "e"});
            7.  把提供的數(shù)組元素放入一個(gè)字符串
            // containing the provided list of elements
            // Apache common lang
            String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
            System.out.println(j);
            // a, b, c
          8.  將一個(gè)數(shù)組列表轉(zhuǎn)換為數(shù)組
            String[] stringArray = { "a", "b", "c", "d", "e" };
            ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
            String[] stringArr = new String[arrayList.size()];
            arrayList.toArray(stringArr);
            for (String s : stringArr)
            System.out.println(s);
            9.  將一個(gè)數(shù)組轉(zhuǎn)換為集(set)
            Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
            System.out.println(set);
            //[d, e, b, c, a]
            10.  逆向一個(gè)數(shù)組
            int[] intArray = { 1, 2, 3, 4, 5 };
            ArrayUtils.reverse(intArray);
            System.out.println(Arrays.toString(intArray));
            //[5, 4, 3, 2, 1]
            11.  移除數(shù)組中的元素
            int[] intArray = { 1, 2, 3, 4, 5 };
            int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
            System.out.println(Arrays.toString(removed));
            12.  將整數(shù)轉(zhuǎn)換為字節(jié)數(shù)組
            byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
            for (byte t : bytes) {
            System.out.format("0x%x ", t);
            }

          posted on 2014-07-15 10:29 順其自然EVO 閱讀(164) 評(píng)論(0)  編輯  收藏 所屬分類: 測試學(xué)習(xí)專欄

          <2014年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 建瓯市| 黎平县| 闸北区| 长宁县| 镇巴县| 武陟县| 西盟| 金昌市| 鸡西市| 黑水县| 镇平县| 来宾市| 永善县| 禄丰县| 白水县| 静安区| 泰顺县| 勐海县| 永胜县| 石渠县| 双峰县| 涟源市| 镇平县| 巨鹿县| 西藏| 翁源县| 明水县| 时尚| 灌阳县| 余姚市| 永胜县| 高平市| 余庆县| 淮安市| 揭东县| 安西县| 德清县| 武穴市| 墨玉县| 和林格尔县| 衡山县|