-1

I just want to sort the array of numbers by the sort method. But unfortunately it throws an error:

java.lang.ArrayIndexOutOfBoundsException:10 at Search.sort(Search.java:30) at Search.main(Search.java:67)

Here is my code:

import java.util.Scanner;

public class Search {

  public Search() {
    System.out.println("inside a constructer");
  }

  public int[] sort(int x[]) {

    for (int i = 0; i < x.length - 1; i++)
      for (int j = 1; j < x.length; j++) {
        int temp;

        if (x[i] > x[j]) {
          temp = x[i];
          x[i] = x[j];
          x[j] = x[i];

        }

      }
    return x;
  }

  public static void main(String[] args) {
    // System.out.println("fgdg"+num);

    int num[] = new int[10];

    Scanner sc = new Scanner(System.in);

    System.out.println("Eneter the 10 integers:");

    for (int i = 0; i < 10; i++) {
      System.out.println("Enter the " + (i + 1) + " number:");
      num[i] = sc.nextInt();
    }

    System.out.println("before sorting:");
    for (int m = 0; m <= num.length; m++)
      System.out.println(num[m]);

    Search obj = new Search();
    int x1[] = obj.sort(num);

    System.out.println("sorted:");

    for (int k = 0; k <= x1.length; k++)
      System.out.println(x1[k]);
  }
}
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
rydz
  • 43
  • 11

5 Answers5

1

Change your for condition to this :

for (int i=0;i<x.length;i++)
    for(int j=1;j<x.length;j++){
       //your code
    }
}

What your code does : It checks every number in array starting from index 0 to size of array.

Lets say you have array of 10 numbers. Then size of array will be 10. And you are iterating from 0 to 10. But numbers are stored in index position 0 to 9. So it will thrown an exception when it try to fetch 10th element from array.

Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
1
int num[] = new int[10]

means that your array has 10 elements(length = 10), indexes from 0 - 9.

change the for loop in the method to

for (int i=0;i<x.length;i++)
    for(int j=i+1;j<x.length;j++) 

!!Change every loop that goes to

<= array.length

into

< array.length 

since the last element of an array is at index array.length - 1.

You also have a mistake where you change the values of x[i] and x[j] in the sort function. It should be:

 if (x[i] > x[j]) {
      temp = x[i];
      x[i] = x[j];
      x[j] = temp;

    }

Your j loop in the sort method has to start from i + 1, not from 1.

DeiAndrei
  • 947
  • 6
  • 16
1

Array indexes are zero-based. Change

for (int i=0;i<=x.length;i++)
    for(int j=1;j<=x.length;j++)

to

for (int i=0;i<x.length;i++)
    for(int j=i;j<x.length;j++)

UPDATE:

Two more places. Change <= to <

for (int m = 0; m <= num.length; m++)
        System.out.println(num[m]);

and

for (int k = 0; k <= x1.length; k++)
        System.out.println(x1[k]);
ponomandr
  • 1,523
  • 13
  • 21
0
for (int i=0;i<=x.length;i++)
        for(int j=1;j<=x.length;j++)
    {
        int temp=0;

        if(x[i]>x[j])
        {
            temp=x[i];
            x[i]=x[j];
            x[j]=x[i];

        }


    }

Should be

for (int i=0;i<x.length;i++)
        for(int j=1;j<x.length;j++)
    {
        int temp=0;

        if(x[i]>x[j])
        {
            temp=x[i];
            x[i]=x[j];
            x[j]=temp;

        }


    }

You are exceeding the size of array!

Indrajith
  • 880
  • 12
  • 21
0

change your for loop to this code:

for (int i=0;i<x.length;i++)
    for(int j=i;j<x.length;j++)
Awtszs
  • 323
  • 1
  • 8
  • 33