首页 > 代码库 > android-ArrayList 使用
android-ArrayList 使用
今天优化一段代码,如下
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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。