线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21649800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:52:16  来源:igfitidea点击:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

javaarrays

提问by user3285999

I am a beginner to programming. I'm a few weeks into my first programming class, so please bear with me. I am not a person to ask for help, so I have searched for an answer extensively with no luck. This is also my first time posting anything in any type of forum, so if my question structure is off I'm sorry and I will correct for future posts.

我是编程的初学者。我的第一堂编程课已经几周了,所以请耐心等待。我不是一个寻求帮助的人,所以我在没有运气的情况下广泛搜索了答案。这也是我第一次在任何类型的论坛上发帖,所以如果我的问题结构不正确,我很抱歉,我会在以后的帖子中更正。

This is the question I am tackling:

这是我要解决的问题:

Write a program that declares an array "alpha" of 50 elements of type "double". Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable. Output the array so that 10 elements per line are printed.

编写一个程序,声明一个包含 50 个“double”类型元素的数组“alpha”。初始化数组,使前 25 个元素等于索引变量的平方,后 25 个元素等于索引变量的三倍。输出数组,以便每行打印 10 个元素。

This is what I got while running the program.

这是我在运行程序时得到的。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:50 on line 23 

And this is the code I'm working on

这是我正在处理的代码

import java.util.*;
import java.lang.*;
public class ProgrammingProblem5_4
{
    public static void main(String[] args)
    { 
        int i=0;
        int count=0;
        double[] alpha = new double[50];


        if (i >= 25)
            alpha[i] = 3*i;
        System.out.print(alpha[i]+ " ");   
        count++;
        if (count==10) {
            System.out.println("/n");
            count=0;
        }
    }
}
}

Thank you in advance for any help. I'm not looking to have this done for me, I'm just stuck and need help finding my way.

预先感谢您的任何帮助。我不想为我完成这件事,我只是被卡住了,需要帮助找到我的路。

回答by user3154554

i don't see any for loop to initalize the variables.you can do something like this.

我没有看到任何 for 循环来初始化变量。你可以做这样的事情。

for(i=0;i<50;i++){
 /* Code which is necessary with a simple if statement*/

   }

回答by Cromax

First you should learn about loops, in this case most suitable is forloop. For instance let's initialize whole table with increasing values starting with 0:

首先你应该了解循环,在这种情况下最合适的是for循环。例如,让我们用从 0 开始的递增值初始化整个表:

final int SIZE = 10;
int[] array = new int[SIZE];
for (int i = 0; i < SIZE; i++) {
    array[i] = i;
}

Now you can modify it to initialize your table with values as per your assignment. But what happen if you replace condition i < SIZEwith i < 11? Well, you will get IndexOutOfBoundException, as you try to access (at some point) an object under index 10, but the highest index in 10-element array is 9. So you are trying, in other words, to find friend's home with number 11, but there are only 10 houses in the street.

现在您可以修改它以根据您的分配使用值初始化您的表。但是,如果你更换条件发生什么i < SIZEi < 11?好吧,IndexOutOfBoundException当您尝试访问(在某个时候)索引为 10 的对象时,您会得到,但 10 元素数组中的最高索引是 9。因此,换句话说,您正在尝试找到编号为 11 的朋友的家,但街上只有10间房子。

In case of the code you presented, well, there must be more of it, as you can not get this error (exception) from that code.

如果是您提供的代码,那么肯定有更多代码,因为您无法从该代码中获取此错误(异常)。

回答by Srinath Ganesh

ArrayIndexOutOfBoundsExceptionin simple wordsis -> you have 10 students in your class (int array size 10) and you want to view the value of the 11th student (a student who does not exist)

ArrayIndexOutOfBoundsException异常简单的话就是- >你有10名学生在班级(int数组大小为10),您要查看的第11届学生的价值(谁不存在的学生)

if you make this int i[3] then i takes values i[0] i[1] i[2]

如果你把这个 int i[3] 那么 i 取值 i[0] i[1] i[2]

for your problem try this code structure

对于您的问题,请尝试此代码结构

double[] array = new double[50];

    for (int i = 0; i < 24; i++) {

    }

    for (int j = 25; j < 50; j++) {

    }

回答by Marc Manzano

I still remember the first weeks of my programming courses and I totally understand how you feel. Here is the code that solves your problem. In order to learn from this answer, try to run it adding several 'print' in the loop, so you can see the progress of the variables.

我仍然记得我的编程课程的前几周,我完全理解你的感受。这是解决您问题的代码。为了从这个答案中学习,尝试在循环中添加几个“打印”来运行它,这样你就可以看到变量的进度。

import java.util.*;
import java.lang.*;

public class foo
{

   public static void main(String[] args)
   { 
      double[] alpha = new double[50];
      int count = 0;

      for (int i=0; i<50; i++)
      {
          // System.out.print("variable i = " + i + "\n");
          if (i < 25)
          {
                alpha[i] = i*i;
          }
          else {
                alpha[i] = 3*i;
          }

          if (count < 10)
          {
            System.out.print(alpha[i]+ " "); 
          }  
          else {
            System.out.print("\n"); 
            System.out.print(alpha[i]+ " "); 
            count = 0;
          }

          count++;
      }

      System.out.print("\n"); 

    }
}