简体   繁体   English

气泡排序程序错误

[英]Error in bubble sort program

I'm getting an error trying to sort array elements using bubble sort algorithm. 我在尝试使用冒泡排序算法对数组元素进行排序时遇到错误。 The error happens after accepting array elements from user. 从用户接受数组元素后,将发生错误。

reference: http://www.java2novice.com/java-sorting-algorithms/bubble-sort/ 参考: http : //www.java2novice.com/java-sorting-algorithms/bubble-sort/

code: 码:

package com.interview.programs;
import java.util.Scanner;
public class BubbleSort
{
    public static void bubble(int array[])
    {
        int n=array.length;
        int k;
        for(int m=n;m>=0;m--)
        {
            for(int i=0;i<n-1;i--)
            {
                k=i+1;
                if(array[i] > array[k])
                {
                    swap(i, k, array);
                }
            }
            print(array);
        }
    }
    private static void swap(int i, int j, int[] array)
    {
        int temp;
        temp=array[i];
        array[i]=array[j];
        array[j]=temp;
    }
    private static void print(int[] array)
    {
        for(int i=0;i<array.length;i++)
        {
            System.out.println(" "+array[i]);
        }
    }
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        BubbleSort b=new BubbleSort();
        Scanner input=new Scanner(System.in);
        int n;
        System.out.println("How many number you want to sort? ");
        n=input.nextInt();
        int[] array = new int[n];
        System.out.println("Enter numbers: ");
        for(int i=0;i<n;i++)
        {
            array[i]=input.nextInt();
        }
        b.bubble(array);
        System.out.println("Your sorted array numbers are: ");
        for(int i=0;i<n;i++)
        {
            System.out.print(+array[i]+" ");
        }
    }
}

output: 输出:

How many number you want to sort? 
3
Enter numbers: 
6
3
9
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at com.interview.programs.BubbleSort.bubble(BubbleSort.java:14)
    at com.interview.programs.BubbleSort.main(BubbleSort.java:50)

in method bubble 方法气泡
for(int i=0;i<n-1;i--)

change i-- to i++ 将我-更改为i ++

Exception java.lang.ArrayIndexOutOfBoundsException is raised when you try to access a negative index from an array or a index bigger than its length. 当您尝试从数组或大于其长度的索引访问负索引时,引发Exception java.lang.ArrayIndexOutOfBoundsException

In this case, you tried to access the index -1 in your second for inside the bubble method - for(int i=0;i<n-1;i--) . 在这种情况下,试图访问索引-1在第二气泡方法内- for(int i=0;i<n-1;i--) Changing i-- to i++ will fix it as you're starting i from 0 and want it to increments until n-1 . i--更改为i++可以解决此问题,因为您是从0开始i并希望其递增直到n-1

在您的for(int i = 0; i <n-1; i--)中将i--更改为i ++

An array index out of bounds exception is what happens when you try to access an item in the array that doesn't exist. 尝试访问数组中不存在的项目时会发生数组索引超出范围异常。 In your case that is element -1. 您的情况是元素-1。 Arrays can only contain elements with indexes greater than 0. 数组只能包含索引大于0的元素。

When you get errors like this, if you read the error it will give you info, and a line number. 当您收到这样的错误时,如果您阅读错误,它将为您提供信息和行号。 Also it's probably better to google for the specific error (eg: what is java.lang.arrayindexoutofboundsexception) than to use stack overflow. 另外,谷歌搜索特定错误(例如:什么是java.lang.arrayindexoutofboundsexception)可能比使用堆栈溢出更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM