【问题标题】:How do I loop through an array and stop at a certain index [closed]如何循环遍历数组并在某个索引处停止 [关闭]
【发布时间】:2020-09-20 02:37:12
【问题描述】:

所以,我有一个包含 7 个数字(0-6)的数组。我想循环它一定次数并停在某个索引或数字处。例如,程序从 0 开始,我想循环它 12 次并让程序输出 4。另一个实例,程序从索引 2 开始并循环 10 次。输出应该是索引 5。我该怎么做?有可能吗?

【问题讨论】:

  • 能否请您发布到目前为止您尝试过的代码?
  • 您好,请更加准确和清楚地说明问题所在,并分享您到目前为止所尝试的内容。这是论坛中的guide about how to ask a good question

标签: java arrays loops for-loop


【解决方案1】:

你可以使用一个简单的循环来控制你如何循环遍历数组。

例子:

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 以外的位置开始,只需在循环之前创建一些代码来设置 index 并记住检查 index 是否小于数组长度

【讨论】:

    【解决方案2】:

    我认为您正在寻找模 % 运算符。这使您可以跳过循环并立即到达最终索引条目。此操作是恒定的,如果您不必实际循环遍历索引直到到达最终数字,则应该首选此操作。

    对于您的两个示例,您可以看到每个案例的两个打印语句:

    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);
      }
    }
    

    【讨论】:

      【解决方案3】:

      "如果程序 (iterations) 从 0 (index) 开始并且我想 循环 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)
      

      “另一个例子,程序从 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]);
      

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

      如果您将 startIndex 值更改为 2loopNumOfTimes 值到 10 那么控制台窗口输出将为:4

      【讨论】:

        猜你喜欢
        • 2017-10-24
        • 2022-06-30
        • 1970-01-01
        • 1970-01-01
        • 2019-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多