java气泡排序if语句超出范围

java气泡排序if语句超出范围,java,arrays,sorting,if-statement,Java,Arrays,Sorting,If Statement,我在运行气泡排序时遇到问题,它没有显示语法错误,每当我运行它时,它在第13行显示java.lang.ArrayIndexOutOfBoundsException,这是if语句 import java.util.*; 公共类数组3{ public static void main(String[] args) { int[] numbers = {1, 5, 2, 6, 3, 8, 9, 4, 7}; int temp = 0; for(int i

我在运行气泡排序时遇到问题,它没有显示语法错误,每当我运行它时,它在第13行显示java.lang.ArrayIndexOutOfBoundsException,这是if语句

import java.util.*;
公共类数组3{

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 + ", ");
    }
}
publicstaticvoidmain(字符串[]args){
int[]数字={1,5,2,6,3,8,9,4,7};
内部温度=0;

对于(int i=0;i删除第一个和第二个for循环中的相等运算符,或者从长度中删除-1

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] + ", ");
    }
}
}
公共类数组3{
公共静态void main(字符串[]args){
int[]数字={1,5,2,6,3,8,9,4,7};
内部温度=0;
//删除第一个for循环中的相等运算符
对于(inti=0;inumbers[j+1]){//这一行有错误
温度=数字[j];
数字[j]=数字[j+1];
数字[j+1]=温度;
}
}
}
//您应该使用数字[i],并删除“=”运算符或在此处使用-1。
for(int i=0;i
这一行表示您在表格中进行迭代。
j
值可以是表格的最后一个标记,因为
numbers.length-1
指的是最后一行(记住表格索引从0开始)


for(int j=0;j这是否回答了您的问题?
j
。谢谢。它删除了越界错误,但显示了输出[I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742,in-System.out.print(数字+“,”),数字表示数组地址([I@15db9742)。你应该加上标记:数字[i]非常感谢你的帮助,我在编程方面相对较新,这样的错误有时对我来说是看不见的。它消除了越界错误,但显示了输出[I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742, [I@15db9742,这是因为您正在打印机for循环中打印整个数组(数字)。请检查for循环的打印。您需要删除for循环中的“=”运算符,并将数字[i]放入。我已经编译了我的答案,它运行良好,示例输出:-1、2、3、4、5、6、7、8、9。@KyleAtienzamy bad我没有更改下面的打印方法,非常感谢您在gme中的帮助
for (int j = 0 ; j <= numbers.length - 1 - i; j++)