【问题标题】:loop is cut one element of the array循环被切割数组的一个元素
【发布时间】:2013-11-12 09:32:34
【问题描述】:

问题是:

编写一个程序,读取一个数字 n,然后声明一个包含 n 个元素的数组。然后程序用前 n 个数字填充数组,其中每个数字都是前一个数字的 2 次方。最后,显示数组的内容。

我的代码:

import java.util.*;

public class Q1 {
  static Scanner scan = new Scanner (System.in);

  public static void main(String args [] ) {
    int num;
    int i = 0;
    System.out.println("Enter a number :");
    num = scan.nextInt();

    double [] a=new double[num];
    a[0]= num ;

    for ( ;i<=a.length-1 ; i++) {
      a[i+1] = Math.pow(2,a[i]);

      System.out.println((int)(a[i]) );
    }
  }
}

错误是:

   ----jGRASP exec: java Q1

Enter a number :
4
4
16
65536
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at Q1.main(Q1.java:16)

 ----jGRASP wedge2: exit code for process is 1.

为什么这么说? 并且用户的数字打印了两次!

【问题讨论】:

  • 我曾经做过这样的家庭作业。
  • 您的错误可能出现在这一行 a[i+1] = Math.pow(2,a[i])
  • 是的,我会直接发送过去

标签: java arrays


【解决方案1】:
 a[i+1] = Math.pow(2,a[i]); // issue here

当i=a.length-1,i+1 是a.length。所以这个数组中没有索引值匹配。

建议:

创建一个拥有lenght= a.length+1的数组

double powArray[] =new double[a.length+1];

现在

powArray[0]=a[0];
for(int i=1;i<powArray.length;i++){
   powArray[i]=Math.pow(2,a[i-1]);
}

【讨论】:

  • @Walaa 你可以从我的回答中得到灵感
【解决方案2】:

问题在第 16 行,根据编译器:a[i+1] = Math.pow(2,a[i]); 问题是这里的索引没有正确匹配。所以它给出了 ArrayIndexOutOfBound 异常。这里是 i=a.length-1, i+1=a.length。所以它给出了 ArrayIndexOutOfBound 异常。

在访问数组项之前,您应该检查您的索引不是负数并且不高于数组长度。

请参考:Avoiding index out of bounds exceptions

【讨论】:

    【解决方案3】:

    专注于您的代码的以下部分,它很容易,如果您自己弄清楚,您会感觉很好:

    for ( ;i<=a.length-1 ; i++) // check from where i starts and where it ends
    {
         a[i+1] = Math.pow(2,a[i]); // check what value of i would be here on each iteration    
         System.out.println((int)(a[i]) );
    }
    

    您也可以使用调试器并检查每次迭代

    【讨论】:

      【解决方案4】:

      这里有几件事可看。

      您已经写了a[0]= num ;,这意味着数组中的第一个值与用户输入的数字相同。因此,您可以再次看到打印的数字。因此,

      a[0] = 1; // initial value has to be 1, contrary to what you've used
      

      接下来,需要修复生成数组值的循环。当你有i+1 时,数组索引在最后一次迭代中超出范围,你的i is a.length - 1 。因此将您的循环更改为这样的内容

      for (int i = 1; i < a.length; i++) {
          a[i] = Math.pow(2, a[i-1]);
      }
      

      现在,在单独的循环中打印数组值,因为之前的循环不会打印数组第一个索引的值。

      for (int i = 0; i < a.length; i++) {
          System.out.println((int)a[i]);
      }
      

      【讨论】:

        【解决方案5】:

        将上限改为a.length-2 .Rest code保持不变

        import java.util.*;
        
        public class Q1 {
          static Scanner scan = new Scanner (System.in);
        
          public static void main(String args [] ) {
            int num;
            int i = 0;
            System.out.println("Enter a number :");
            num = scan.nextInt();
        
            double [] a=new double[num];
            a[0]= num ;
        
            for ( ;i<=a.length-2 ; i++) {
              a[i+1] = Math.pow(2,a[i]);
        
              System.out.println((int)(a[i]) );
            }
          }
        }
        

        【讨论】:

          【解决方案6】:
                  int num ;
                  System.out.println("Enter a number :");
                  num = scan.nextInt();
                  double [] a=new double[num];
                  a[0]= num ; //Assign First Element To Array
                  double prev = a[0];
                  for(int i=1;i<a.length;i++){
                      a[i] = Math.pow(2,prev);
                      prev = a[i];
                  }
                  for(int i=0;i<a.length;i++)
                      System.out.println(a[i]);
          

          输出

              Enter a number :
              3
          
              3.0
              8.0
              256.0
          

          【讨论】:

            【解决方案7】:

            只需将循环更改为:

            for(;i a.length-1;i++)

            自从你指定 4 作为 a[] 的长度时..

            您有 a[0],a[1],a[2],a[3]... 4 个插槽。

            现在 a.length 给出 4。

            所以对于您的 a[i+1] for i=a.length-1 (i.e 3 ) .. a[i+1] = a[4]... 这超出了为您的数组绑定的数组.

            【讨论】:

              【解决方案8】:
              import java.util.*;
              
              public class Q1 {
                static Scanner scan = new Scanner (System.in);
              
                public static void main(String args [] ) {
                  int num;
                  int i = 0;
                  System.out.println("Enter a number :");
                  num = scan.nextInt();
              
                  double [] a=new double[num];
              
                  for ( ;i<a.length ; i++) {
                    a[i] = Math.pow(2,i);
              
                    System.out.pri``nt((int)a[i]+ " ");
                  }
                }
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2015-04-18
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-09-05
                • 2014-05-25
                • 1970-01-01
                相关资源
                最近更新 更多