用Indef和StringDef代替枚举

本文探讨了枚举在Java中的使用及其内存占用问题,介绍了Google官方推出的IntDef注解作为枚举的一种替代方案,并通过示例展示了如何在Android开发中应用IntDef。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近一直在了解一些性能优化上的知识,有一条是这样的:

不要过多的使用枚举,枚举占用的的内存空间比整型大。

那不使用枚举,那怎么搞呢?首先,我们要清楚,枚举有什么用。知道枚举的用处以后我们才能根据它的用处来代替它。

看一下这里:http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html,有这么一段话:

 

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Because they are constants, the names of an enum type's fields are in uppercase letters.

枚举类型是一种特殊的数据类型,一组已经定义好了的常量,变量必须等于已经预定义的常量之一。……枚举必须是大写……

还有这段:

Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type. For example, this code from the Planet class example below iterates over all the planets in the solar system.

Java中枚举类型比其他的同行在其他语言中更强大。当创建了一个枚举的时候,编译器会自动添加一些特殊的方法,例如,他们有一个静态值方法,该方法返回一个包含所有声明它们顺序枚举值的数组。

相对于int静态常量来说,枚举最大的优势还是提供了类型安全。

那怎么办呢?查了资料,发现谷歌官方也注意到了这点,官方推出了两个注释:IntDef和StringDef,用来提供编译器到类型检查来替代枚举的作用。

来看一下Android Developers给的官方解释:

Denotes that the annotated element of integer type, represents a logical type and that its value should be one of the explicitly named constants. If the IntDef#flag() attribute is set to true, multiple constants can be combined.

表示该整数类型的注释元素,代表着一种逻辑类型,它的价值应该是明确的命名常量之一。如果IntDef标志的属性设置为true,多种常量可以结合起来。

根据官方给的解释,是不是给人一种跟枚举的功能基本差不多的感觉。ok ,下面我们来看一下怎么去使用这个注解:

还是直接上代码:

 

public class MainActivity extends Activity {
public static final int MALE = 0;
public static final int FEMALE = 1;
@Bind(R.id.button)
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    Person person = new Person();
    person.setSex(MALE);
    button.setText(person.getSexDes());
}
class Person {
    @SEX
    private int sex;
    public void setSex(@SEX int sex) {
        this.sex = sex;
    }
    @SEX
    public int getSex() {
        return sex;
    }
    public String getSexDes() {
        if (sex == MALE) {
            return "男";
        } else {
            return "女";
        }
    }
}

@IntDef({MALE, FEMALE})
@Retention(RetentionPolicy.SOURCE)
public @interface SEX {
}
}

 

 

很麻烦的一点是:当我们直接赋值一个int 给setSex()的话,编译器会报错,但是代码是没有问题的,仍然可以正确运行,这就牵扯到一个Android Studio的静态代码检查问题了,不知道的同学可以去了解一下。

 

 

很简单,很实用,对吧!
 

 

 

 

 

 

### 关于约瑟夫问题的枚举优化方法 #### 1. 约瑟夫问题简介 约瑟夫问题是经典的计算机科学问题之一,描述了一组人围成一圈并按照一定规则淘汰的过程。最终目标是找到最后一个剩余的人的位置。 对于该问题的传统解法通常采用模拟的方式,时间复杂度较高 \(O(n \times k)\),其中 \(n\) 是人数,\(k\) 是步数。为了提高效率,可以通过数学推导数据结构优化来降低计算成本[^1]。 --- #### 2. 数学公式的应用 通过递推公式可以显著减少计算量。假设总共有 \(n\) 个人,每次报到第 \(m\) 个人被淘汰,则最后剩下人的位置可以用以下递推关系表示: \[ f(1) = 0 \] \[ f(i) = (f(i-1) + m) \% i, \quad \text{for } i > 1 \] 这种递推方式的时间复杂度仅为 \(O(n)\)。 以下是基于上述公式的 C++ 实现: ```cpp #include <iostream> using namespace std; const int M = 3; int main() { int n, f = 0; cin >> n; for (int i = 1; i <= n; ++i) { f = (f + M) % i; } cout << f + 1 << endl; } ``` Python 的实现如下: ```python M = 3 def josephus(n): f = 0 for i in range(1, n + 1): f = (f + M) % i return f + 1 if __name__ == "__main__": n = int(input()) print(josephus(n)) ``` Java 版本则为: ```java import java.util.Scanner; public class JosephusProblem { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = 3; int f = 0; for (int i = 1; i <= n; i++) { f = (f + m) % i; } System.out.println(f + 1); } } ``` 以上三种语言均采用了相同的逻辑,即利用递推公式快速求解结果。 --- #### 3. 枚举优化策略 当面对较大的输入规模时,单纯的暴力枚举可能无法满足性能需求。此时可考虑以下几种优化手段: ##### (1)动态规划预处理 如果多次查询不同大小的约瑟夫环问题,可以预先计算出较小范围内的结果存储在一个数组中,后续直接查表即可。这种方法特别适合重复调用场景下的加速操作[^2]。 ##### (2)分治思想的应用 针对超大规模的数据集 (\(n>1e9\)) ,传统线性扫描变得不可行。一种有效的方法是将整个序列分割成若干子区间分别独立解决后再合并答案。具体做法依赖二叉树或其他高效索引机制完成定位过程。 ##### (3)位运算技巧 某些特殊情况下,比如固定间隔长度等于2的情况(\(m=2\)), 可以借助二进制特性进一步简化表达式: 设当前轮次编号为 \(t\) , 则有: \[ pos_t=(pos_{t−1}<<1)+1 \] 这里使用左移代替乘法提高了运行速度. --- #### 4. 总结 综上所述,在实际开发过程中应根据具体情况选择合适的解决方案。对于中小规模的问题推荐直接运用迭代公式;而对于极端情况需引入更高级别的抽象工具辅助分析与建模工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值