有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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


共 (3) 个答案

  1. # 1 楼答案

    我认为您正在寻找模%运算符。这允许您跳过循环并立即到达最终索引项。这个操作是恒定的,如果你不需要在最后一个数字之前在索引中循环,应该优先使用

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

    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);
      }
    }
    
  2. # 2 楼答案

    "If the program (iterations) starts at 0 (index) and I want to loop it 12 times and have the program output 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)
    

    "Another instance, the program starts at index 2 and loops 10 times. The output should be index 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]);
    

    在上面的代码中,您可以看到开始索引(startIndex)是0,并且loopNumOfTimes变量中所需的循环数(迭代次数)是12。控制台窗口的输出将是:4

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

  3. # 3 楼答案

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

    例如:

    int arr[]; // Our array
    int t; // How much to cycle through the array
    /**
    * Some code to set a value for t and fill arr
    **/
    
    int index = 0; // The final index. Initally 0 to prevent undefined case  t <= 0
    
    for (int i = 0; i < t; ++i) {
       if (index == arr.lenght) {index = -1;} // -1 so whe don't need to have an else clause
       ++index;
    }
    
    

    如果您想在数组中从不同于0的位置开始,只需创建一些代码,在循环之前设置索引,并记住检查索引是否小于数组长度