Java 如何循环数组并在某个索引处停止

Java 如何循环数组并在某个索引处停止,java,arrays,loops,for-loop,Java,Arrays,Loops,For Loop,我有一个7个数字的数组,0-6。我想循环它一段时间,然后停在某个索引或数字上。例如,程序从0开始,我想循环12次,让程序输出4。另一个例子是,程序从索引2开始,循环10次。输出应该是索引5。我如何做到这一点?这可能吗?您可以使用一个简单的循环来控制如何在阵列中循环 例如: int-arr[];//我们的阵列 int t;//在阵列中循环多少次 /** *设置t值和填充arr的一些代码 **/ int index=0;//最后的索引。初始值为0以防止未定义的情况t我认为您正在寻找模%运算符。这允许

我有一个7个数字的数组,0-6。我想循环它一段时间,然后停在某个索引或数字上。例如,程序从0开始,我想循环12次,让程序输出4。另一个例子是,程序从索引2开始,循环10次。输出应该是索引5。我如何做到这一点?这可能吗?

您可以使用一个简单的循环来控制如何在阵列中循环

例如:

int-arr[];//我们的阵列
int t;//在阵列中循环多少次
/**
*设置t值和填充arr的一些代码
**/

int index=0;//最后的索引。初始值为0以防止未定义的情况t我认为您正在寻找模
%
运算符。这允许您跳过循环并立即到达最终索引项。这个操作是恒定的,如果你不需要在最后一个数字之前在索引中循环,应该优先使用

对于两个示例,您可以看到每种情况下的两个打印语句:

class Main {
  public static void main(String[] args) {
    int arr[] = {0,1,2,3,4,5,6};
    int n = arr.length;
    int t = 12;
    //case 1
    System.out.println(arr[t%n]);

    //case 2
    int index = 2;
    t = 10;
    System.out.println((t + index)%n);
  }
}
“如果程序(迭代)从0(索引)开始,并且我希望 循环12次,程序输出4“

好的,我明白了。从索引0开始,以文字1开始计数:

1  2  3  4  5  6  7  8  9  10  11  12   (on the 12th iteration starting from Index 0)
-------------------------------------
0  1  2  3  4  5  6  0  1  2   3   4    (element value at index)
-------------------------------------
0  1  2  3  4  5  6  0  1  2   3   4    (array index)
“另一个例子是,程序从索引2开始,循环10 次数。输出应为索引5

这个让我困惑:

      1  2  3  4  5  6  7  8  9  10     (on the 10th iteration starting from Index 2)
-----------------------------------
0  1  2  3  4  5  6  0  1  2  3  4      (element value at index)
-----------------------------------
0  1  2  3  4  5  6  0  1  2  3  4      (array index)
输出也应该是索引4,而不是索引5

无论如何,还有另一种方法可以实现:

int startIndex = 0;                     // The INDEX value to start iterations from.
int loopNumOfTimes = 12;                // The literal number of desired iterations.
int[] array = {0, 1, 2, 3, 4, 5, 6};    // The Integer Array (length 7).
int counter = 0;                        // A counter used to count literal iterations.
int i;                                  // Decalred outside of loop so its value can be used.
// Iterate through the Array
for (i = startIndex; i < array.length; i++) {
    counter++;  // Increment counter.
    // Have we reached the desire number of iterations?
    if (counter == loopNumOfTimes) {
        // Yes...Break out of loop.
        break;
    }
    /* Reset the 'for' loop if we've reached actual array length (length minus 1).
       i++ in the 'for' loop is automatically applied once the first iteration is 
       complete and every iteration thereafter as long as 'i' remains less than 
       the literal length of the array. Because we are applying a value change to 
       'i' so as to start the loop form 0 again (a loop reset) the i++ will be 
       immediately be applied which takes 'i' to 1 istead of the desired 0. This 
       is no good so we set 'i' to -1 that way when i++ is applied 'i' is set to 
       0 and iterations start again from that index value.                  */
    if (i == (array.length - 1)) {
        i = -1;
    }
}
    
// Display the Array element value located at index 'i'.
System.out.println(array[i]);
int startIndex=0;//从中开始迭代的索引值。
int loopNumOfTimes=12;//所需迭代的文字数。
int[]数组={0,1,2,3,4,5,6};//整数数组(长度为7)。
int计数器=0;//用于计算文字迭代次数的计数器。
int i;//在循环外部贴花,以便可以使用其值。
//遍历数组
对于(i=startIndex;i
在上面的代码中,您可以看到开始索引(startIndex)是0,并且loopNumOfTimes变量中所需的循环数(迭代次数)是12。控制台窗口的输出将为:
4


如果将startIndex值更改为2,将loopNumOfTimes值更改为10,则控制台窗口输出将为:
4

您能发布您迄今为止尝试过的代码吗?您好,请更准确、更清楚地说明问题所在,并分享您迄今为止的尝试。这里是论坛上的一篇文章