Home java Error java.lang.ArrayIndexOutOfBoundsException

Error java.lang.ArrayIndexOutOfBoundsException

Author

Date

Category

Code throws an error

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0 at mass.Uporyadoc.main (Uporyadoc.java:34)

/ * An array of n elements is given. Order array in ascending order * /
package mass;
import java.util.Scanner;
import static java.lang.System. *;
public class Uporyadoc {
  public static void sort (int [] a)
  {
    int i, j, n = 0, k;
    for (i = 0; i & lt; n; i ++) {
      for (j = i; j & lt; n; j ++) {
        if (a [j] & lt; a [i]) {
          k = a [j];
          a [j] = a [i];
          a [i] = k;
        }
      }
    }
    for (i = 0; i & lt; n; i ++)
      out.println (a [i] + "");
    out.println ();
  }
  public static void main (String [] args) throws Exception
  {
    Scanner reader = new Scanner (in);
    int i;
    int n = 0;
    int a [] = new int [n];
    out.println ("Enter the number of items:");
    n = reader.nextInt ();
    for (i = 0; i & lt; n; i ++)
      a [i] = (int) (Math.random () * 11);
      out.println ("Output");
      sort (a);
  }
}

Answer 1, authority 100%

int n = 0;
int a [] = new int [n];

You are creating an array of length 0 . Then, read the number from the console into the n variable:

n = reader.nextInt ();

But the array has already been created by this moment. Further iteration over the array immediately leaves the array.


Also, the sort () method will not work as you expect.

At the very beginning, you declare n = 0 :

public static void sort (int [] a)
  {
    int i, j, n = 0, k;

and immediately start looping from 0 to & lt; n . Replace n = 0 with n = a.length .


Answer 2, authority 33%

It seems to me that you absolutely do not understand what exactly the code you provided does. Try to understand the basics of working with arrays first. Also see what selection (or bubble) sort is and how it works.

public static void sort (int [] a) {
  int n = a.length; // get the dimension of the array a
  for (int i = 0; i & lt; n; i ++) {// i & lt; n - counter i must be less than the dimension of array a
    for (int j = i; j & lt; n; j ++) {
      if (a [j] & lt; a [i]) {
        int k = a [j];
        a [j] = a [i];
        a [i] = k;
      }
    }
  }
  for (i = 0; i & lt; n; i ++)
    out.println (a [i] + "");
  out.println ();
}
public static void main (String [] args) throws Exception {
  Scanner reader = new Scanner (in);
  int n = 0;
  out.println ("Enter the number of items:");
  n = reader.nextInt (); // first get the value n
  int a [] = new int [n]; // then create an array with dimension n
  for (int i = 0; i & lt; n; i ++)
    a [i] = (int) (Math.random () * 11);
  out.println ("Output");
  sort (a);
}

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