English 中文(简体)
如果发言涉及多个
原标题:Multiple if statements

假设我使用一个语句, 说明十种可能不同的按键, 将事件值发送给活动听众:

public boolean onTouch(int v) { //this is my only listener for all ten buttons
  if(event.getAction() == MotionEvent.ACTION_DOWN){
   if(v==button_one){pool.play(bass1, 1f,1f, 1, 0, 1f);}
   if(v==button_two){pool.play(bass2, 1f,1f, 1, 0, 1f);}
   if(v==button_three){pool.play(bass3, 1f,1f, 1, 0, 1f);}
   if(v==button_four){pool.play(snare1, 1f,1f, 1, 0, 1f);}
   if(v==button_five){pool.play(snare2, 1f,1f, 1, 0, 1f);}
   if(v==button_six){pool.play(snare3, 1f,1f, 1, 0, 1f);}
   if(v==button_seven){pool.play(hh1, 1f,1f, 1, 0, 1f);}
   if(v==button_eight){pool.play(hh2, 1f,1f, 1, 0, 1f);}
 }
 return false;
}

将这些分类是否更有效率?比如... 一个点击事件是圈圈的,一个点击事件是低音的,一个点击事件是低音的,一个点击事件是hi hats的,这样当按下按钮时,程序就不必在每次声明中计数,如果声明的话,只有一个听众在听发射事件吗?

问题回答

像这样的东西呢?

HashMap<int,int>  soundMap = new HashMap<int,int>();
soundMap.put(button_one, bass1);
soundMap.put(button_two, bass2);
soundMap.put(button_three, bass3);
soundMap.put(button_four, snare1);
soundMap.put(button_five, snare2);
soundMap.put(button_six, snare3);
soundMap.put(button_seven, hh1);
soundMap.put(button_eight, hh2);

将哈什马普作为类变量, 并在 Create 上初始化映射 。 然后您可以使用它作为听众使用 :

public boolean onTouch(int v) {
    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        pool.play(soundMap.get(v), 1f, 1f, 1, 0, 1f);
    }
    return false;
}

这样做的好处是,如果你今后需要添加更多的按钮,你只需用新的声音映射修改地图初始化方法;听众不需要任何修改。

来补充丰富的建议和观点, 并且同意之前所有关于“这真的是一个性能问题”的观点, 我将选择一种结构, 使代码最容易阅读和保存给你, 以及任何需要维护它的人。 通过维护, 也考虑延长它。 三个月后, 当你想要添加5个音效垫时会发生什么?

减到最低限度的线条数, 可能会给您一些几乎无法满足的性能提升, 在您的APK中省下几字节, 但在大多数情况下, 我用易读性来交换。

但是,如果你的经验水平不同, 你不能只是看着它说"啊,是的,我明白了", 然后保持你的开关,或者更好。

在您知道有问题之前,我不会担心打破它。一个开关有助于减少重复的 < code> if 语句:

public boolean onTouch(int v) {
  if(event.getAction() == MotionEvent.ACTION_DOWN) {
    switch (v) {
    case button_one: pool.play(); break;
    case button_two: pool.play(..); break;
    ...
    }
  }
 return false;
}

本案的绩效并不值得担心;有条件声明所需时间将完全被周围的事件处理代码所忽略。 第一至第三套绩效法是衡量、计量、度量,而我对发现差异的可能性持怀疑态度。

我忍不住注意到,唯一能改变的就是第一种集合. play 的争论。 Bass1, Bass2, et secret 和相应的 v 值之间有关系吗?

我认为它没有必要,但我会明确建议转换和大小写。 你可以将它作为最普通的交换和大小写。 你可以将它订为最普通的交换和大小写,但这样的小变化是不会被注意的。 如果发言的话,经过这些变化不会花很长时间。

I don t know how many times this if runs, but it doesn t seem inefficient to me. This boolean test is very fast. Anyway, if you do want to make it as efficient as possible, I see two options:

  1. The easy one: use else if instead of if.
  2. The complicated one: use an array of ActionIf objects to do what you want:

public interface ActionIf {
    public void go();
}

public class ActionBass1 implements ActionIf {
    @Override
    public void go() {
        pool.play(bass1, 1f,1f, 1, 0, 1f);
    }
}

public class ActionBass2 implements ActionIf {
    @Override
    public void go() {
        pool.play(bass2, 1f,1f, 1, 0, 1f);
    }
}

...

public ActionIf[] actions = {new ActionBass1(), new Action Bass2(), ...);

public boolean onTouch(int v) { //this is my only listener for all ten buttons
    if(event.getAction() == MotionEvent.ACTION_DOWN && v >= 0 && v <= (button_eight-button_one)){
        actions[button_one+v].go();
    }
    return false;
}





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...