首页 > 代码库 > 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 使用