-3

I'm having trouble running bubble sort, it shows no syntax errors and whenever I run it shows a java.lang.ArrayIndexOutOfBoundsException on line 13 which is the if statement,

import java.util.*;

public class Array3 {

public static void main(String[] args) {


    int[] numbers = {1, 5, 2, 6, 3, 8, 9, 4, 7};
    int temp = 0;       


    for(int i = 0 ; i <= numbers.length - 1 ; i++) {                        
        for (int j = 0 ; j <= numbers.length - 1 - i; j++) {
            if(numbers[j] > numbers[j + 1]) { //error is in this line
                temp = numbers[j];
                numbers[j] = numbers[j + 1];
                numbers[j + 1] = temp;
            }
        }
    }
    for(int i = 0 ; i <= numbers.length ; i++) {
        System.out.print(numbers + ", ");
    }
}

}

Manikandan
  • 1,195
  • 8
  • 26
  • 3
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – jhamon Dec 18 '19 at 09:42
  • `j < numbers.length - 1 - i`. – luk2302 Dec 18 '19 at 09:42

2 Answers2

0

Remove the equal operator in first and second for loop Or remove the -1 from length.

public class Array3 {

public static void main(String[] args) {


    int[] numbers = {1, 5, 2, 6, 3, 8, 9, 4, 7};
    int temp = 0;       

    //Remove the equal operator in first for loop   
    for(int i = 0 ; i < numbers.length - 1 ; i++) {  
     //Remove the equal operator in second for loop                      
        for (int j = 0 ; j < numbers.length - 1 - i; j++) {
            if(numbers[j] > numbers[j + 1]) { //error is in this line
                temp = numbers[j];
                numbers[j] = numbers[j + 1];
                numbers[j + 1] = temp;
            }
        }
    }
   // you should use number[i],and remove '=' operator or use -1 here.
    for(int i = 0 ; i < numbers.length ; i++) {
        System.out.print(numbers[i] + ", ");
    }
}
}
Manikandan
  • 1,195
  • 8
  • 26
  • thankyou it removed the out of bounds error but it shows the output [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, – Kyle Atienza Dec 18 '19 at 10:35
  • Its because you are printing the entire array (numbers) in the printer for loop . please check the print for loop. you need to remove the '=' operator in the for loop and put the number[i]. – Manikandan Dec 18 '19 at 10:38
  • I have compiled my answer, it's working fine, sample output:- 1, 2, 3, 4, 5, 6, 7, 8, 9. @KyleAtienza – Manikandan Dec 18 '19 at 10:49
  • 1
    my bad i havent changed the print method below, thankyou very much for helpin gme – Kyle Atienza Dec 18 '19 at 11:01
0

This line means that you iterate through your table. j value can be the last indice of the table because numbers.length - 1 refers to the last row (remember table indices start at 0).

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

When you do numbers[j + 1], if j value is on the last indice of the table, j + 1 will refer to a row out of the table which throws the java.lang.ArrayIndexOutOfBoundsException.

Changing your loop condition from j <= numbers.length -1 -i to j < numbers.length -1 -i will solve your problem.

Degravef
  • 120
  • 8
  • thankyou it removed the out of bounds error but it shows the output [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, – Kyle Atienza Dec 18 '19 at 10:34
  • in System.out.print(numbers + ", "), numbers refers to the array address ([I@15db9742). You should add the indice : numbers[i] – Degravef Dec 18 '19 at 10:47
  • thankyou very much for helping i am relatively new in programming and mistakes like this are sometimes unnoticeable to me – Kyle Atienza Dec 18 '19 at 11:00