如何从某个索引循环数组然后结束呢? [英] How do I loop on array from a certain index then end at it?

查看:111
本文介绍了如何从某个索引循环数组然后结束呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循某种算法,所以每次开始循环数组时,我都需要从某个索引开始并从该特定索引开始搜索数组,直到数组结束,如果没有找到它正在寻找它将从阵列的开始直到该指数开始。



我尝试过:



for(i = ptr; i< sizeOfTheArray; i ++)

如果它找到了它想要的东西,它会破坏,否则

for(i = 0; i< ptr; i ++)

有没有办法在一个循环中实现该逻辑?

I'm following a certain algorithm, so every time I start looping the array I need to start at a certain index and search the array starting from that certain index till the end of the array and if didn't find what it was looking for it's going to start from the start of the array till that index.

What I have tried:

for ( i = ptr ; i < sizeOfTheArray; i++)
if it found what it wants, it's going to break, else
for ( i = 0 ; i < ptr ; i++)
is there a way to achieve that logic in one loop?

推荐答案

首先设置要搜索的最大项目数

Start by setting maximum number of items to search
int maxChecks = sizeOfTheArray;
for (int i = ptr; maxChecks > 0; i++, maxChecks--)
   {
   if (i >= sizeOfTheArray) i = 0;
   ...do your search...
   }


这篇关于如何从某个索引循环数组然后结束呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆