【问题标题】:Exception in thread java.lang.ArrayIndexOutOfBoundsException: 5线程 java.lang.ArrayIndexOutOfBoundsException 中的异常:5
【发布时间】:2015-02-27 22:40:20
【问题描述】:

我是新手,正在尝试完成以下教程

    // Create a method called countEvens
    // Return the number of even ints in the given array. 
    // Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1. 
/*
 * SAMPLE OUTPUT:
 *  
 * 3
 * 0
 * 2
 *  
 */

下面是我的代码

public static void main(String[] args) {

        int a[] = {2, 1, 2, 3, 4};
         countEvens(a); // -> 3
         int b[] = {2, 2, 0};
         countEvens(b); // -> 3
         int c[] = { 1, 3, 5};
         countEvens(c); // -> 0

    }


    public static void countEvens(int[] x){
        int i = 1;
        int count = 0;
        while ( i <= x.length){
            if (x[i] % 2 == 0){
                count ++;
            }
            i ++;
        }
        System.out.println(count);
    }

代码可以运行,但我收到以下错误消息

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at apollo.exercises.ch05_conditionals.Ex5_CountEvens.countEvens(Ex5_CountEvens.java:23)
    at apollo.exercises.ch05_conditionals.Ex5_CountEvens.main(Ex5_CountEvens.java:10)

我可以知道我在这里做错了什么吗?

【问题讨论】:

    标签: java arrays if-statement while-loop


    【解决方案1】:

    线

    while ( i <= x.length)
    

    应该是

    while ( i < x.length)
    

    如果xlength是5,例如,索引是0、1、2、3和4。索引从0到比数组长度小一。

    不过最简洁的方法是使用 for each 循环而不是 while 循环:

    public static void countEvens(int[] x) {
        int count = 0;
        for (int number : x)
            if (number % 2 == 0)
                count++;
        System.out.println(count);
    }
    

    【讨论】:

      【解决方案2】:

      'i'应该从0到length()-1,因为数组索引从0开始,最后一个元素的索引是length()-1。

      因此,您的代码的正确版本应该是:

      public static void countEvens(int[] x){
          int i = 0;
          int count = 0;
          while ( i < x.length){
              if (x[i] % 2 == 0){
                  count ++;
              }
              i ++;
          }
          System.out.println(count);
      }
      

      对于您的特定目的,for 循环会更简单。

      for(int i = 0; i< x.length(); i++){
        if(x[i]%2==0){
        count++;
        }
      }
      

      【讨论】:

        【解决方案3】:

        while ( i &lt;= x.length),您将循环直到i 等于x 的长度。数组的最后一个索引总是 length - 1,因此将小于或等于 (&lt;=) 更改为小于 (&lt;)。此外,将 i 初始化为 0,因为 Java 数组是从零开始的。

        【讨论】:

        • 问题不是在数组上迭代太多次,而是数组是从零开始的,而 OP 假设从 1 开始
        • 确实如此。用i - 1 掩盖它是一个糟糕的解决方案。
        • 实际上不是。 OP 迭代 5 次,但是 1~5 而不是 0~4。不是太多次,而是在错误的位置。我同意 i-1 并不漂亮,但它是最快的解决方案。但你是对的。从 0 开始迭代到 i
        【解决方案4】:

        Java(和大多数其他语言)中的数组从 0 开始索引。但是数组的长度是数组中元素的计数。所以对于你的数组 a[]

        int a[] = {2,1,2,3,4};
        

        索引增加到4,而长度为5。

        由于&lt;=(小于等于)运算符,您正在从 1~5 迭代数组,但数组的索引是 0~4,因此您得到一个索引超出范围错误。当您访问数组的每个元素时,您应该使用

        if (x[i-1] % 2 == 0) //iterates from 0~4 rather than 1~5
        

        否则你可以设置迭代器int i = 0;并使用小于&lt;操作符来保证迭代器移动0~4

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-02-19
          • 2012-01-13
          • 2011-09-09
          • 2016-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多