Java 最小化开关箱

Java 最小化开关箱,java,android,eclipse,Java,Android,Eclipse,我正在创建一个从文本文件中读取歌词的应用程序。有没有办法减少这种代码,因为开关盒可以达到数百个 int itemPosition=position; itemName=song[position]; Intent openViewLyric=new Intent(this, ViewLyric.class); switch(itemPosition) { case 0: openViewLyric.putExtra

我正在创建一个从文本文件中读取歌词的应用程序。有没有办法减少这种代码,因为开关盒可以达到数百个

    int itemPosition=position;
    itemName=song[position];
    Intent openViewLyric=new Intent(this, ViewLyric.class);

    switch(itemPosition)
    {
        case 0:
            openViewLyric.putExtra("song", itemName);
            openViewLyric.putExtra("resid", R.raw.above_all);
            startActivity(openViewLyric);
            break;

        case 1:
            openViewLyric.putExtra("song", itemName);
            openViewLyric.putExtra("resid", R.raw.age_to_age);
            startActivity(openViewLyric);
            break;

        case 2:
            openViewLyric.putExtra("song", itemName);
            openViewLyric.putExtra("resid", R.raw.as_the_deer);
            startActivity(openViewLyric);
            break;

您的案例使用不同的数据重复相同的代码

您可以从地图中提取数据,然后编写一次代码。即使使用稀疏数组,贴图也可以工作。如果已知数组相当满,则可以使用基元数组

例如,如果R.raw是枚举类,则可以对其进行一次初始化:

 private static Map<Integer,R.raw> s_mapPositionToData 
     = new HashMap<Integer,R.raw>();
 static {
     // Fill the map
 }
在包含类型的帮助下,可以将itemName放在同一个映射中。例如,您可以声明包含名称和R.raw的类型ExtraSongData,然后将映射声明为map。

创建查找列表:

private static List<Object> resids = new ArrayList<Object>(){{
  add(R.raw.above_all);
  add(R.raw.age_to_age);
  .
  .
  .
}}
编辑

看来你唯一的选择就是使用哪一个收藏。当我选择列表作为第一个列表时,我将基于以下事实来支持列表:它不是一个数组,并且假设itemPosition总是小于或等于列表大小,那么映射结构对于您的目的来说太多了。

在假设R是R.raw的类型时,尽早初始化R数组,将数组中的位置映射到特定的R值

R[] allRs = new R[] {
    R.raw.above_all,
    R.raw.age_to_age,
    R.raw.as_the_deer
};
然后,您可以将整个switch语句替换为:

openViewLyric.putExtra("song", itemName);
openViewLyric.putExtra("resid", allRs[itemPosition]);
startActivity(openViewLyric);

为什么不用所有的值填充地图呢。这可以在构造函数、onCreate方法甚至静态方法中完成:

Map<Integer, Integer> songsMapping = new HashMap<Integer, Integer>();
songsMapping.put(0, R.raw.above_all);
songsMapping.put(1, R.raw.age_to_age);
songsMapping.put(2, R.raw.as_the_deer);

善意的问候

唯一似乎正在改变的是R.raw额外的。您能否创建一个包含所有这些内容的数组,并使用该位置获取资源。例如putExtraresid,myRawArray[位置];写一个这样做的函数,它将最小化代码的重复。非常感谢。。我从乔伊斯·法雷尔(Joyce Farrell)的《JavaTm编程》一书中学习了Java,但这本书从未提到过地图、稀疏数组。你有什么书建议我读吗?Regards@Tuss-了解一般的数据结构、Java中的数据结构以及Java中的良好实践会很有帮助。对于数据结构,我个人喜欢Sedgewick的算法和Cormen等人的算法介绍。有关Java中的数据结构,请参阅。《核心Java 1》一书还包括一节关于集合的内容,以及您需要了解的其他概念。有关良好实践,请获取并阅读Bloch的有效Java.@Tuss-另外,欢迎使用StackOverflow。如果您发现答案或问题有用,可以单击左侧的向上箭头。如果你认为这是最好的答案,你可以点击左边的复选框,非常感谢!!我的代码现在短多了。
openViewLyric.putExtra("song", itemName);
openViewLyric.putExtra("resid", allRs[itemPosition]);
startActivity(openViewLyric);
Map<Integer, Integer> songsMapping = new HashMap<Integer, Integer>();
songsMapping.put(0, R.raw.above_all);
songsMapping.put(1, R.raw.age_to_age);
songsMapping.put(2, R.raw.as_the_deer);
int itemPosition=position;
Intent openViewLyric=new Intent(this, ViewLyric.class);
openViewLyric.putExtra("song", song[position]);
openViewLyric.putExtra("resid", songsMapping.get(itemPosition));
startActivity(openViewLyric);