I have a java loop that I need to execute code on a specific point. I created an array to store the values at that I need the loop to execute at but am unsure how to todo this within the loop.
private int [] countdownValues = {22,42,62,82,102,122};
Pseudo code would look like
for(int i=0; i++)
if(i == countdownValue)
... else {}
Thank you for your time.
It sounds like you're just asking how to check if an array contains a value? Something like this?:
for (int i = 0; i < someValue; i++) {
if(Arrays.binarySearch(countdownValues, i) > 0) {
// perform a specific action
} else {
// perform the normal action
}
}
Do you mean something like this?
// Iterate through the array
for(int i = 0; i < countdownValues.length; i++)
{
if(i == countdownValue[specific point])
// execute your code here
}
Something like this?
int start = 0;
for (int end : countdownValues) {
int i;
for (i = start; i < end; i++) {
// action(s) from then
}
// action(s) from else
// start just after the end index next time
start = end+1;
}
This assumes the values are all non-negative without duplicates and that they are inascending order.
You can just use foreach
loop following way:
for (int value: countdownValues) {
doStuff(value);
}
This way you won't be running unnecessary loops for values not in array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.