android-ArrayList 使用

來源:互聯網
上載者:User

標籤:

今天最佳化一段代碼,如下

int num = 0;             boolean skipAppend = false;            int types_ext1[] = new int[] {                    ModuleType.TYPE_CONTACT, ModuleType.TYPE_MESSAGE,                    ModuleType.TYPE_PICTURE, ModuleType.TYPE_BOOKMARK,ModuleType.TYPE_MUSIC            };               int types_ext2[] = new int[] {                    ModuleType.TYPE_CONTACT, ModuleType.TYPE_MESSAGE,                    ModuleType.TYPE_PICTURE, ModuleType.TYPE_BOOKMARK,ModuleType.TYPE_CALENDAR            };            int types_ext3[] = new int[] {                    ModuleType.TYPE_CONTACT, ModuleType.TYPE_MESSAGE,                    ModuleType.TYPE_PICTURE, ModuleType.TYPE_BOOKMARK            };if(mIsGoogleOrigCalendarExist && mIsGoogleOrigMusicExist){            num = types.length;            for (int i = 0; i < num; i++) {                types_ext[i] = types[i] ;            }            MyLogger.logE("yjp", "music & calendar are not 3rd-party");        } else if(!(mIsGoogleOrigCalendarExist || mIsGoogleOrigMusicExist)){            num = types_ext3.length;            for (int i = 0; i < num; i++) {                types_ext[i] = types_ext3[i];            }            MyLogger.logE("yjp", "music & calendar are 3rd-party");        } else if(!mIsGoogleOrigCalendarExist && mIsGoogleOrigMusicExist){            num = types_ext1.length;            for (int i = 0; i < num; i++) {                types_ext[i] = types_ext1[i];            }            MyLogger.logE("yjp", "calendar is 3rd-party");        } else if(mIsGoogleOrigCalendarExist && !mIsGoogleOrigMusicExist){            num = types_ext2.length;            for (int i = 0; i < num; i++) {                types_ext[i] = types_ext2[i];            }            MyLogger.logE("yjp", "music is  3rd-party");        }

這段代碼,其實就是改變int[]裡面的元素。因為int[]數組就是增刪比較麻煩,只能靠迴圈提取到另外的數組。所以,我想到了使用list,這樣動態改變起來就非常的方便

如下

 ArrayList<Integer> typeList  = new ArrayList<Integer>();            for (int i = 0; i < types.length; i++) {                typeList.add(types[i]);            }                try {                 PackageManager pm1 = getPackageManager();                 pm1.getPackageInfo("com.android.calendar", PackageManager.GET_ACTIVITIES);                 mIsGoogleOrigCalendarExist = true;             } catch (PackageManager.NameNotFoundException e) {                         MyLogger.logE("yjp", "google original Calendar not found");             }                try {                 PackageManager pm2 = getPackageManager();                 pm2.getPackageInfo("com.android.music", PackageManager.GET_ACTIVITIES);                 mIsGoogleOrigMusicExist = true;             } catch (PackageManager.NameNotFoundException e) {                         MyLogger.logE("yjp", "google original Music not found");             }                if (!mIsGoogleOrigMusicExist) {                typeList.remove(Integer.valueOf(ModuleType.TYPE_MUSIC));            }                if (!mIsGoogleOrigCalendarExist) {                typeList.remove(Integer.valueOf(ModuleType.TYPE_CALENDAR));            }                if (FeatureOption.VANZO_OOS_BROWSER_SUPPORT) {                typeList.remove(Integer.valueOf(ModuleType.TYPE_BOOKMARK));            }                int num = typeList.size();

 

android-ArrayList 使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.