Java 如何从数组中获取第一个数字并对其执行操作

Java 如何从数组中获取第一个数字并对其执行操作,java,android,arrays,Java,Android,Arrays,我在开始时生成几个随机数4,然后如果用户选择下一个选项级别5,依此类推。。。我正在研究如何检查数组中第一个生成的数字是什么,并执行一个操作,然后在该操作完成后移动到数组中的第二个数字,依此类推。 生成的数字始终为1到4,但数量会发生变化 ImageButton 1= (ImageButton) findViewById(R.id.imageButton1); ImageButton 2= (ImageButton) findViewById(R.id.imageButton2); ImageBu

我在开始时生成几个随机数4,然后如果用户选择下一个选项级别5,依此类推。。。我正在研究如何检查数组中第一个生成的数字是什么,并执行一个操作,然后在该操作完成后移动到数组中的第二个数字,依此类推。 生成的数字始终为1到4,但数量会发生变化

ImageButton 1= (ImageButton) findViewById(R.id.imageButton1);
ImageButton 2= (ImageButton) findViewById(R.id.imageButton2);
ImageButton 3= (ImageButton) findViewById(R.id.imageButton3);
ImageButton 4= (ImageButton) findViewById(R.id.imageButton4);
↑ 这些是我想根据数组中的第一个数字突出显示的图像按钮 假设生成的4个数字是[2,1,2,4] 我想要

我希望上面的颜色改变1秒,然后移动到数组中的下一个颜色

这里是生成数字和代码的地方

int value;
        TextView textTest = (TextView)findViewById(R.id.textViewTesting);

        StringBuilder builder = new StringBuilder();

        int[] NumbersArray = new int[20];

        for (int num = 0; num < 4 +temp; num++) 
{
            value = (int) (Math.random() * 4 + 1);

            builder.append(value + " ");
            textTest.setText(builder.toString());
            NumbersArray[num] = value;
            System.out.println(NumbersArray[num]);
}

数值不是有效的变量名

 ImageButton 1= (ImageButton) findViewById(R.id.imageButton1);
这是无效的

试试这个:

ArrayList<ImageButton> listOfButtons = new ArrayList<ImageButton>();


//adds R.id.imageButton1 at first position of your list
listOfButtons.add((ImageButton) findViewById(R.id.imageButton1));

//adds R.id.imageButton5 at second position of your list
listOfButtons.add((ImageButton) findViewById(R.id.imageButton5));

//adds R.id.imageButton3 at thirdposition of your list
listOfButtons.add((ImageButton) findViewById(R.id.imageButton3));


//firstItem is R.id.imageButton1
ImageButton firstItem = listOfButtons.get(0);

//secondItem is R.id.imageButton5
ImageButton secondItem = listOfButtons.get(1);
....
int index;//index should never exceed list size
....
ImageButton selectedByIndex = listOfButtons.get(index);

关于数值,我很抱歉。我的代码中有它们作为文本,但您上面提到的代码仍然不适用于我,我在索引中得到错误,在selectedByIndex中得到错误,在…=新的ArrayList让你知道该怎么做。显然,在我的示例中索引并没有初始化,我不知道要检索哪个索引。如果希望第一个元素索引为0;如果希望第二个元素索引=1,如果希望最后一个元素索引=listOfButtons.size-1;此外,为了使ArrayList正常工作,您显然需要导入java.util.ArrayList;我导入了ArrayList,但我不知道如何以及在何处将您的建议放入我的代码中。在过去的3周里,我一直在做android java,对于如何实现我想做的事情,我几乎没有什么想法。根据我上面的代码,你能告诉我如何根据数组中的数字改变按钮的颜色吗?我不知道你想做什么,我也不能为你写完整的代码。事实上,这不是为什么。我在这里所做的是向您展示如何利用ArrayList,它允许您在案例ImageButton中存储任意数量的任意类型元素,并在以后随机访问您想要的任何索引。这将消除您在编译时声明大量变量或可能耗尽空间的数组的需要。由于您是新手,我将在我的答案中添加一些细节以帮助您解决问题。在本网站上,您应该在收到答案时选择最佳答案。
ArrayList<ImageButton> listOfButtons = new ArrayList<ImageButton>();


//adds R.id.imageButton1 at first position of your list
listOfButtons.add((ImageButton) findViewById(R.id.imageButton1));

//adds R.id.imageButton5 at second position of your list
listOfButtons.add((ImageButton) findViewById(R.id.imageButton5));

//adds R.id.imageButton3 at thirdposition of your list
listOfButtons.add((ImageButton) findViewById(R.id.imageButton3));


//firstItem is R.id.imageButton1
ImageButton firstItem = listOfButtons.get(0);

//secondItem is R.id.imageButton5
ImageButton secondItem = listOfButtons.get(1);
....
int index;//index should never exceed list size
....
ImageButton selectedByIndex = listOfButtons.get(index);