简体   繁体   English

尝试使用冒泡排序将随机整数数组从最大到最小排序

[英]Trying to sort an array of randomized integers from biggest to smallest using a bubble sort

So as part of a task, I was asked to create an array with randomized values within a range, and then to sort it from smallest to biggest (I used a bubble sort), and then to firstly, print the sum of all the elements in the array, then to list them from smallest to biggest. 因此,作为任务的一部分,我被要求创建一个具有一定范围内随机值的数组,然后将其从最小到最大排序(我使用冒泡排序),然后首先打印所有元素的总和在数组中,然后从最小到最大列出它们。

My issue is that I keep getting the ArrayIndexOutOfBoundsException error, but cannot find where this problem lies. 我的问题是,我不断收到ArrayIndexOutOfBoundsException错误,但找不到此问题所在。

You can see in the code that I've put in the randomArrays method, a for loop that creates the random values for the array size I declared in the main method, then, underneath the for loop, I've created an if statement that checks to see if an element's value is bigger than the element after it, if it is, it swaps the place of the elements, until they're all sorted into smallest to biggest, and the loop is terminated. 您可以在我放在randomArrays方法中的代码中看到一个for循环,该循环为我在main方法中声明的数组大小创建随机值,然后在for循环下创建了一个if语句,检查一个元素的值是否大于其后的元素,如果是,它将交换元素的位置,直到将它们全部按最小到最大排序,并终止循环。

Any help is much appreciated, thank you :) 任何帮助都非常感谢,谢谢:)

public class MyArray {

public static void main(String[] args) {
    int[] elements = new int[50];
    int min = 0;
    int max = 50;

    randomArrays(elements, max, min);

}

public static void randomArrays(int[] elements, int max, int min) {
    int range = max - min; //defines the range of the integers
    int temp;
    boolean fixed = false;

    while (fixed == false) {
        fixed = true;

        for (int i = 0; i < elements.length; i++) {
            elements[i] = min + (int) (Math.random() * range);
            while (i < elements.length) {
                if (elements[i] > elements[i + 1]) {
                    //if 8   >    5
                    temp = elements[i + 1];
                    //store 5 in temp
                    elements[i + 1] = elements[i];
                    //put the 8 in the 5's place
                    elements[i] = temp;
                    fixed = false;
                }
                i++;
            }

        }
    }

}
//System.out.println(elements[i]);
}

My issue is that I keep getting the ArrayIndexOutOfBoundsException error, but cannot find where this problem lies. 我的问题是,我不断收到ArrayIndexOutOfBoundsException错误,但找不到此问题所在。

Problem lies in the condition of the for loop . 问题在于for loop的条件。 You get ArrayOutOfBounds exception when i=49 and then you try to access i+1 index which doesn't exists. i=49时,您将获得ArrayOutOfBounds异常,然后尝试访问不存在的i+1索引。

Change 更改

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

to

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

As you can already see that your code is going out of the arrays limit. 正如您已经看到的那样,您的代码超出了数组的限制。 if you look at your code, following is where this is happening 如果您查看您的代码,以下是发生这种情况的地方

while (i < elements.length) {

Its this while loop part, so if you change it to correctly loop thru the right number of elements, your problem will be resolved..change your while loop code with this one 它是while循环的一部分,因此,如果将其更改为通过正确数量的元素正确地进行循环,您的问题将得到解决。

while (i < elements.length-1) {

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

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