java - 在java,android中使用循环计数器访问变量

标签 java android loops counter

如何在循环中使用计数器访问声明的变量,如下所示:

声明的变量:

Button square0, square1, square2, square3, square4, square5, square6,
        square7, square8, temp;

使用这样的循环访问它们:

for (int i = 0; i < 9; i++) {
    (Button) ("square"+i).setBackgroundResource();

最佳答案

如果不诉诸一些真正可疑的魔法,你就无法做到这一点(我什至不认为你可以通过 Java 中的反射来做到这一点)。

使用 array相反。

关于java - 在java,android中使用循环计数器访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10252312/

相关文章:

java - Android:在 xml 中声明颜色

Android Studio 构建失败 - 任务 ':app:dexDebug' 执行失败

android - 如何在 android 中的 facebook 上发布图像?

android - 识别由于 Android 操作系统导致的应用程序运行缓慢

javascript - useInterval 和 React hooks 的问题 - 无限循环

java - 在 jar 中加载字体作为资源失败

java - 哈夫曼树字符串表示

java - 如何<sws :annotation-driven/> with marshaller works

java - 我需要帮助在 Java 中实现某种循环情况

python - 在 Python 中同时迭代多个列表