How to get the first value in array, then second and so on?

I generate and store numbers in an array (these numbers are 1 to 4) I want to have a loop that checks if the first number is 1 and if it is do action, if its not move on to 2 and so on..

final ArrayList<Integer> listInteger = new ArrayList<>();
final int[] NumbersArray = new int[20];
for (int generatedNumbers = 0; generatedNumbers < 2 + temp; generatedNumbers++) {
        value = (int) (Math.random() * 4 + 1);
        NumbersArray[generatedNumbers] = value;

I tried

for(Integer i : NumbersArray) {
                    if (i == 1) {
                        System.out.println("1");
Color1.setBackgroundColor(Color.parseColor("#FF7F7500"));
                      }
                   else if (i == 2) {
                        else if (i == 2) {
                        System.out.println("2");
Color2.setBackgroundColor(Color.parseColor("#FF0000"));
                     }

Answers


for(Integer i : listInteger){
   if(i == 1){
    // do smth
    break; // quit loop you found your number 1
    }
}

This is how you loop over it.


Need Your Help

the Difference between running time and time of obtaining results in CUDA

cuda gpu nvidia

I am trying to implement My algorithm on GPU using CUDA. this program work well but there is a problem. when I try to print out the results, they will be shown too late .