Home java Why does java.lang.ArrayIndexOUTOFBOUNDSException occurs?

Why does java.lang.ArrayIndexOUTOFBOUNDSException occurs?

Author

Date

Category

This code makes the following:

  • fills two array random values ​​from 1 to 40
  • fills the 3rd array with the sums of the values ​​of the corresponding indices of the 1st and 2nd arrays
  • calculates the arithmetic arithmetic 3rd array and prints its minimum and maximum value.

When executing the code, an error appears:

Exception in Thread “Main” java.lang.ArrayIndExOUTOFBOUNDSEXCEPTION: 40
& nbsp; & nbsp; & nbsp; at massives.massives.main (Massives.java:37)
Java Result: 1

Code itself:

import java.util.random; // Import the necessary static library
Public Class Massives {
  / **
   * @Param Args The Command Line Arguments
   * /
  Public Static Void Main (String [] Args) {
    IN I, P, M, S, D, G, C, K, MAX, MIN;
    Random Random1 = New Random (1);
    Random Random2 = New Random (1);
    int [] array1 = new int [40];
    int [] array2 = new int [40];
    int [] array3 = new int [40];
      d = 0;
      for (i = 0; i & lt; array1.length; i ++)
        Array1 [i] = random1.nextint (40);
      for (p = 0; p & lt; array2.length; p ++)
        array2 [i] = random1.nextint (40);
      for (m = 0; m & lt; array3.length; m ++)
        array3 [m] = array1 [m] + array2 [m];
      For (S = 0; S & LT; Array3.Length; S ++)
        D + = Array3 [s];
      G = D / Array3.Length;
      max = 0;
      For (C = 0; C & LT; Array3.Length; C++)
        If (Array3 [i] & GT; MAX)
          Max = Array3 [i];
      MIN = Array3 [39];
      for (k = 39; k & gt; 0; k--)
        MIN = Array3 [k];
      System.Out.printLN (MAX);
      System.out.printLN (MIN);
      System.Out.printLN (G);
    // Todo Code Application Logic Here
  }
}

Answer 1, Authority 100%

Carefully feel about working with cycles.

Public Class Massives {
  Public Static Void Main (String [] Args) {
  IN I, P, M, S, D, G, C, K, MAX, MIN;
  Random Random1 = New Random (1);
  Random Random2 = New Random (1);
  int [] array1 = new int [40];
  int [] array2 = new int [40];
  int [] array3 = new int [40];
    d = 0;
    for (i = 0; i & lt; array1.length; i ++)
      Array1 [i] = random1.nextint (40);
    for (p = 0; p & lt; array2.length; p ++)
      array2 [p] = random1.nextint (40);
    for (m = 0; m & lt; array3.length; m ++)
      array3 [m] = array1 [m] + array2 [m];
    For (S = 0; S & LT; Array3.Length; S ++)
      D + = Array3 [s];
    G = D / Array3.Length;
    max = 0;
    For (C = 0; C & LT; Array3.Length; C++)
      If (Array3 [C] & GT; MAX)
        Max = Array3 [C];
    MIN = Array3 [39];
    for (k = 39; k & lt; array3.length; k--)
      MIN = Array3 [k];
    System.Out.printLN (MAX);
    System.out.printLN (MIN);
    System.Out.printLN (G);
  // Todo Code Application Logic Here
  }
}

Answer 2, Authority 100%

for (k = 39; c & lt; array3.length; k--)
      MIN = Array3 [k];

You have a mistake in the cycle condition, change the with on k .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions