特定点上的Java循环
我有一个java循环,需要在特定点上执行代码。我创建了一个数组来存储需要循环执行的值,但不确定如何在循环中执行特定点上的Java循环,java,arrays,loops,Java,Arrays,Loops,我有一个java循环,需要在特定点上执行代码。我创建了一个数组来存储需要循环执行的值,但不确定如何在循环中执行 private int [] countdownValues = {22,42,62,82,102,122}; 伪代码看起来像 for(int i=0; i++) if(i == countdownValue) ... else {} 谢谢你抽出时间 听起来您只是在问如何检查数组是否包含值?像这样的 for (int i = 0; i < someValue; i++) {
private int [] countdownValues = {22,42,62,82,102,122};
伪代码看起来像
for(int i=0; i++)
if(i == countdownValue)
... else {}
谢谢你抽出时间 听起来您只是在问如何检查数组是否包含值?像这样的
for (int i = 0; i < someValue; i++) {
if(Arrays.binarySearch(countdownValues, i) > 0) {
// perform a specific action
} else {
// perform the normal action
}
}
for(int i=0;i0){
//执行特定的操作
}否则{
//执行正常操作
}
}
你的意思是这样的吗
// Iterate through the array
for(int i = 0; i < countdownValues.length; i++)
{
if(i == countdownValue[specific point])
// execute your code here
}
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;
}
//遍历数组
对于(int i=0;i
像这样的东西
// Iterate through the array
for(int i = 0; i < countdownValues.length; i++)
{
if(i == countdownValue[specific point])
// execute your code here
}
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;
}
int start=0;
用于(整数结束:倒计时值){
int i;
for(i=start;i
这假设值都是非负的,没有重复项,并且它们是按顺序排列的。您可以按照以下方式使用
foreach
循环:
for (int value: countdownValues) {
doStuff(value);
}
这样,您就不会对不在数组中的值运行不必要的循环。您能更清楚地说明一下吗?请您详细说明一下。我正在努力将它实现到我的代码中。谢谢找不到符号数组?@Razsberi:如果还没有,您可能需要导入
java.util.Arrays
。课程记录在这里:谢谢,解决了这个问题。但是,使用代码并不是每次i=数组中的值时都执行特定操作。有什么想法吗?@Razsberi:当你在调试器中单步执行时,countdownValues
和i
的值是多少?如果您仅在调试器中对这些值执行条件语句,它将返回什么?请检查此项并自行运行。。for循环未在数组中设置的my值处停止:/