Java方法从数组列表返回

时间:2014-02-27 17:35:56

标签: java android arrays optimization arraylist

我想知道这是否可能?以及做这样的事情的最佳方式是什么:

    private int NotifCountBg(int countstyle) {
    return new int[]{
            R.drawable.count_bevel,
            R.drawable.count_blue,
            R.drawable.count_green,
            R.drawable.count_orange,
            R.drawable.count_pink,
            R.drawable.count_purple,
            R.drawable.count_red,
            R.drawable.count_gray
         }[countstyle];
    }

2 个答案:

答案 0 :(得分:3)

您可以为数组使用常量:

private static final int[] STYLES = new int[]{ R.drawable.count_bevel, R.drawable.count_blue, 
    R.drawable.count_green, R.drawable.count_orange, R.drawable.count_pink,
    R.drawable.count_purple, R.drawable.count_red, R.drawable.count_gray };

private int notifCountBg(final int countstyle) {
    return STYLES[countstyle];
}

答案 1 :(得分:1)

使用枚举:去除索引,数组和an-typed(int)常量。