有没有替代方法来改变for循环中的索引?

时间:2015-01-29 05:49:18

标签: for-loop

假设我不想在随机数中使用7,所以我这样做。我知道这不是一个好例子。对不起,第一次。

int temp[8];

for (int i = 0 ; i < 8 ; i ++ ){
    temp[i] = random(8);
    if (temp[i] == 7){
        temp[i] = random(8);
        i--;
    }
}

我的理想是不做我在for循环中的位置。使用额外的空间来记录它然后在第一个for循环之外做一些事情。代码将变为:

ArrayList i_index;
int count = 0 ;
int temp[8];

for (int i = 0 ; i < 8 ; i ++ ){
    temp[i] = random(8);
    if (temp[i] == 7){
        i_index.add(i) ;    
    }
}

for (int i = 0 ; i < 8 ; i ++ ){
    if ( i_index.get(count) == i ){
        temp[i] ++ ; //do something or whatever just not return 7 
        count ++ ;  
    }
}

这是正确的还是有更好的理想? 或者希望有人能给出一个很好的例子。

4 个答案:

答案 0 :(得分:1)

我没有看到像这样操纵索引有什么不妥。我经常这样做。

例如,最近我不得不在数组中找到可能成对出现的项目,但并不总是如此,所以循环看起来与此类似(它的伪代码):

for(int i = 0; i < itemCount; i++)
{
    item1 = items[i]
    if (i + 1 < itemCount)
    {
       item2 = items[i+1]
       if (item2 is a pair to item1)
       {
           ...do something
           i++ // item2 is already processed so skip it next time by increasing the i
       }
    }
}

如果你的算法需要它,我会说完全可以操纵i。有时没有别的办法(比如我的例子)。


编辑:既然您向我们展示了完整的案例,我仍然认为可以更改算法中的i。但是,还有一些其他解决方案可能:

示例-1 :您可以在while-loop循环中使用for

for (int i = 0 ; i < 8 ; i ++ )
{
    int num = random(8);
    while(num == 7)
    {
        num = random(8);
    }
    temp[i] = num;
}

示例-2 :或者在do-while循环中使用for反之亦然

for (int i = 0 ; i < 8 ; i ++ )
{
    int num;
    do
    {
        num = random(8);
    } 
    while (num == 7);

    temp[i] = num;
}

示例-3 :或只是一个do-while循环,直到找到8个与7不同的数字:

int count = 0;
int num;

do
{
    num = random(8);
    if (num != 7)
    {
        temp[count] = num;
        count++;
    }
}
// Repeat as long as there are fewer then 8 items or the number is 7
while (count < 8 || num == 7);

示例-4 :如果您可以使用除数组之外的其他集合,例如列表或可以添加项目的集合,它甚至可以更简单:

int num;
bool isValidNumber;
do
{
    num = random(8);
    isValidNumber = (num != 7);
    if (isValidNumber)
    {
        numList.add(num);
    }
}
// Repeat as long as there are fewer then 8 items or the number is invalid
while (numList.count < 8 || !isValidNumber);

答案 1 :(得分:0)

我通常做的是使用你在代码中显示的第二个索引或计数器变量。

count=0;
for(i=0,i<800,i++)
{
    if(x)
    {
       //do something using count
       //ex: array[count] = i+3*count;
       count++;
    }
}

我不认为你的第二个循环可能是必要的,但你也不清楚你想要做什么。无论如何,这就是你在for循环中使用辅助计数器的方式。

答案 2 :(得分:0)

我想以这种方式尝试,因为不确定您将使用什么语言以及XXX和YYY行动的含义。

PHP中的示例

$arr=array();

    for($x=0;$x<8;$x++)
    {
        if(xxx)
        {
            array_push($arr,$x);
        }
    }

现在我们有一个与XXX条件匹配的数组,所以我们只根据它存储在数组中的内容进行YYY操作。

foreach($arr as $y)
{
    YYY($y);
}

答案 3 :(得分:0)

我相信你会尝试使用相同的i再次运行循环(不会在循环体中减少它)。一种选择是将增量移动到循环体中并使用continue之类的

for (int i = 0 ; i < 8 ; ) { // <-- remove the i++ from here
    if (xxx){
        YYY; // do something
        // i--; // <-- don't need to subtract one from i
        continue; // <-- will rerun the loop with the same i
    }
    i++; // <-- add it here.
}

另一种选择是else喜欢

for (int i = 0 ; i < 8 ; ) { // <-- remove the i++ from here
    if (xxx){
        YYY; // do something
        // i--; // <-- don't need to subtract one from i
    } else {
        i++; // <-- add it here.
    }
}

然后你不需要continue;

根据您新近更新的帖子

for (int i = 0 ; i < 8 ; i ++ ){
    temp[i] = random(7); // <-- 0,6 (will never be 7).
}

如果你想要一个差距,我建议你仍然缩小范围并进行测试,如

for (int i = 0 ; i < 8 ; i ++ ){
    int r = random(8); // <-- assuming it's 8 exclusive,
    temp[i] =  (r != 7) ? r : 8; // <-- skip 7.
}
相关问题