当前位置: 首页 > 面试题库 >

我如何优化包含某些重复行的代码?

屠锐
2023-03-14
问题内容

我在Android编程中有以下代码

Button btn1 = ( Button ) findViewById( R.id.btn1 );
Button btn2 = ( Button ) findViewById( R.id.btn2 );
Button btn3 = ( Button ) findViewById( R.id.btn3 );
Button btn4 = ( Button ) findViewById( R.id.btn4 );
Button btn5 = ( Button ) findViewById( R.id.btn5 );
Button btn6 = ( Button ) findViewById( R.id.btn6 );
Button btn7 = ( Button ) findViewById( R.id.btn7 );
Button btn8 = ( Button ) findViewById( R.id.btn8 );
Button btn9 = ( Button ) findViewById( R.id.btn9 );

并持续到btn30
在python中,我通过以下简单代码对其进行优化

#is a python syntax (for_statement)
#python work by tab
for i in range(1,31):
    #in python not need to declare temp
    temp="""Button btn"""+str(i)+"""=(Button)findViewById(R.id.btn"""+str(i)+""")"""
    exec(temp)#a default function in python

在Java编程中我该怎么做?或者我可以做到吗?是否存在简单的代码?

UPDATE 1

所以有两种方法可以做到
Code 1

final int number = 30;
final Button[] buttons = new Button[number];
final Resources resources = getResources();

for (int i = 0; i < number; i++) {
    final String name = "btn" + (i + 1);
    final int id = resources.getIdentifier(name, "id", getPackageName());

    buttons[i] = (Button) findViewById(id);
}

Code 2

public static int getIdByName(final String name) {
    try {
        final Field field = R.id.class.getDeclaredField(name);

        field.setAccessible(true);
        return field.getInt(null);
    } catch (Exception ignore) {
        return -1;
    }
}

final Button[] buttons = new Button[30];

for (int i = 0; i < buttons.length; i++) {
    buttons[i] = (Button) findViewById(getIdByName("btn" + (i + 1)));
}

其他方法是GidView
Tanks All。


问题答案:

您可以创建一个由组成的数组,Button并使用getIdentifiermethod,该方法允许您通过名称获取标识符。

final int number = 30;
final Button[] buttons = new Button[number];
final Resources resources = getResources();

for (int i = 0; i < number; i++) {
    final String name = "btn" + (i + 1);
    final int id = resources.getIdentifier(name, "id", getPackageName());

    buttons[i] = (Button) findViewById(id);
}

如果有人感兴趣如何仅使用Java获得相同的结果

上面的解决方案使用Android特定的方法(例如getResourcesgetIdentifier),通常无法使用Java,但是我们可以使用reflection和编写一种类似于a的方法getIdentifier

public static int getIdByName(final String name) {
    try {
        final Field field = R.id.class.getDeclaredField(name);

        field.setAccessible(true);
        return field.getInt(null);
    } catch (Exception ignore) {
        return -1;
    }
}

然后:

final Button[] buttons = new Button[30];

for (int i = 0; i < buttons.length; i++) {
    buttons[i] = (Button) findViewById(getIdByName("btn" + (i + 1)));
}


 类似资料:
  • 问题内容: 我正在尝试从文本文件中读取文本,读取行,删除包含特定字符串的行(在这种情况下为“坏”和“顽皮”)。我写的代码是这样的: 我这样写,但没有成功。 重要的一件事是,如果文本的内容是这样的: 我不希望输出有空行。所以不喜欢: 但是像这样: 我应该从上面的代码中编辑什么? 问题答案: 您可以像这样使代码更简单,更易读 使用上下文管理器和任何。

  • 我有一个熊猫数据框,有4行4列-这里是一个简单的版本: 我想做的是把它转换成一个2*8的数据帧,每个数组都有B、C和D——所以它看起来像这样: 在阅读熊猫文档时,我尝试了以下方法: 但是给了我一个错误,我无法识别源(以 DataError:没有要聚合的数字类型 ) 接下来,我想根据一个值分割数据帧,但我认为.groupby命令可能会处理它

  • 我这里有这个html 我想检查span类是否包含值2013。我应该如何编写xpath? 这些是我到目前为止尝试过的,根据Firepath,它们要么无效,要么没有匹配的节点。

  • 问题内容: 这个问题已经在这里有了答案 : 熊猫将一些列转换为行 (4个答案) 2年前关闭。 我有一个熊猫数据框,有4行4列-这是asimple版本: 我想做的是将其转换为2 * 8数据帧,并对每个数组使用B,C和D Alligng-因此它看起来像这样: 在阅读熊猫文档时,我尝试了以下方法: 但给我一个错误,我无法识别来源(结尾为 DataError:没有要聚合的数字类型 ) 接下来,我想基于A值

  • 以下是特定于模板类类型的函数,如何自定义模板代码,使其不针对某些类型进行编译?如果问题不清楚,请看这个例子。 上述类应适用于以下类型: 如您所见,没有,因此我得到一个编译错误,表示未定义

  • Flyway在其留档中说明了可重复迁移的一些用法: 用法:(重新)创建视图/过程/函数/包/... 我想在可重复迁移中创建一些触发器/函数,这些稍后会在版本迁移中引用,并应用到表中。 Flyway最后运行可重复的迁移,这意味着触发器在被引用时不存在。 是否可以在版本化迁移之前运行某些可重复的迁移? 是否不支持此用例,因为自动更新应用于表的触发器是不好的做法?