【Java】按位存储:使用int存储boolean数组

有一种场景,比如App设置页中会有一组开关选项,这个时候保存这些开关的状态,如果每个按钮都对应一个boolean值的话,太大材小用显得鸡肋,频繁读取SharedPreferences 存取效率自然快不过一次读取。

首先,敲定每个boolean值存储的位置

1
2
3
4
5
private int mBroadcastCustomValue = 0;   //用来存储的int值
public static final int BROADCAST_TYPE_CUSTOM_BASE = 1;
public static final int BROADCAST_TYPE_CUSTOM_TRAFFIC = 1 << 1;
public static final int BROADCAST_TYPE_CUSTOM_CAMERA = 1 << 2;
public static final int BROADCAST_TYPE_CUSTOM_SAFE = 1 << 3;

一、添加Add

1
2
3
4
5
6
private void addLevel(int level){
mBroadcastCustomValue |= level; //add
}

//调用方式如下
addLevel(BROADCAST_TYPE_CUSTOM_BASE);

二、删除Delete

1
2
3
4
5
6
private void deleteLevel(int level){
mBroadcastCustomValue ^= mBroadcastCustomValue & level; //delete
}

//调用方式如下
deleteLevel(BROADCAST_TYPE_CUSTOM_BASE);

三、读取Read

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 从value中读取level的设置值,level即是某个boolean值的位置
*/
private boolean isLevelAccess(int value, int level){
if((value & level) == level){
return true;
}
return false;
}

//调用方式如下
boolean a = isLevelAccess(mBroadcastCustomValue, BROADCAST_TYPE_CUSTOM_BASE);

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2019 iTimeTraveler All Rights Reserved.

访客数 : | 访问量 :