如何修复这个冒泡排序程序?

发布于 2024-08-09 01:51:43 字数 877 浏览 7 评论 0原文

package arraySort;

import java.io.IOException;
import java.io.File;
import java.util.*;

public class openFile {
    int x;
    static int i;
    static int[] myList = {100};

    public static void main(String[] args){
        try{
            File myFile = new File("arraySort.txt");
            Scanner scan = new Scanner(myFile);
            while(scan.hasNext()){                
                myList[i] = scan.nextInt();
                BubbleSort(myList);
                System.out.println(myList[i]);                                
         } 
         catch(IOException e){
             System.out.println("File not found!");
         }
    }
    public static void BubbleSort(int[] x){
        if (x[i] > x[i + 1]){
            int temp;
            temp = x[i];
            x[i] = x[i+1];
            x[i+1] = temp;
        }
    }
}
package arraySort;

import java.io.IOException;
import java.io.File;
import java.util.*;

public class openFile {
    int x;
    static int i;
    static int[] myList = {100};

    public static void main(String[] args){
        try{
            File myFile = new File("arraySort.txt");
            Scanner scan = new Scanner(myFile);
            while(scan.hasNext()){                
                myList[i] = scan.nextInt();
                BubbleSort(myList);
                System.out.println(myList[i]);                                
         } 
         catch(IOException e){
             System.out.println("File not found!");
         }
    }
    public static void BubbleSort(int[] x){
        if (x[i] > x[i + 1]){
            int temp;
            temp = x[i];
            x[i] = x[i+1];
            x[i+1] = temp;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘虞初梦 2024-08-16 01:51:43

您的程序有几个问题,不仅仅是与排序部分有关。

static int[] myList = {100};

此行将 myList 定义为大小为 1 的数组,包含单个元素 100。然后,您的主循环是

while(scan.hasNext()) {
    myList[i] = scan.nextInt();
    BubbleSort(myList);
    System.out.println(myList[i]);
}

您在此循环中不增加 i ,因此您只是用从文件中读取的任何值覆盖 myList 中的单个值。当您的 Bubblesort 函数尝试访问 myList[i+1] 时,它会抛出 ArrayIndexOutOfBoundsException,因为索引 处没有元素i+1(等于 1,因为您不增加 i)。

一般来说,特别是对于初学者来说,使用 ArrayList 比使用常规数组更好。此外,您应该首先填充数组,并且只有在数组包含所有元素后才尝试对其进行排序。最后,将变量设置为本地变量而不是类成员是一个更好的主意。这样一来,您的 main 函数就会类似于

ArrayList myList = new ArrayList();
while(scan.hasNext()) {
    myList.append(scan.nextInt());
}
Bubblesort(myList);

然后更改 Bubblesort 以采用 ArrayList,然后您还可以使循环索引 i 属于Bubblesort 方法。完成后,您可以致力于使冒泡排序算法发挥作用。请记住要小心数组索引,以免访问数组边界之外。

Your program has several problems, not just related to the sorting part.

static int[] myList = {100};

This line defines myList as an array with size 1, containing the single element 100. Then, your main loop is

while(scan.hasNext()) {
    myList[i] = scan.nextInt();
    BubbleSort(myList);
    System.out.println(myList[i]);
}

You do not increase i in this loop so you are just overwriting the single value in myList with whatever value you read from the file. And when your Bubblesort function tries to access myList[i+1], it throws an ArrayIndexOutOfBoundsException because there is no element at index i+1 (which equals 1, since you don't increase i).

In general, and especially for a beginner, it is better to use ArrayList than a regular array. Also, you should fill in the array first and only after it has all the elements should you attempt to sort it. Finally, it is a better idea to make the variables local instead of class members. So that would make your main function something like

ArrayList myList = new ArrayList();
while(scan.hasNext()) {
    myList.append(scan.nextInt());
}
Bubblesort(myList);

And then change Bubblesort to take an ArrayList, and then you can also make the loop index i local to the Bubblesort method. After that is done, you can work on getting the bubble sort algorithm working. Remember to be careful with your array indices there so that you never access outside the bounds of the array.

我最亲爱的 2024-08-16 01:51:43

这里没有直接给您答案,而是提供一些提示:

  1. 您在 BubbleSort() 中没有任何循环。

  2. 您应该只在读入文件中的所有数字之后调用BubbleSort()一次。意思是,将调用移到 while 循环之外。

  3. 您永远不会增加变量i,因此您每次通过while循环时都会覆盖myList[0]

  4. 数组的大小不可调整。如果您尝试分配给 myList[1]myList[2],您将收到 ArrayIndexOutOfBoundsException 错误。有多种方法可以解决此问题 - 一种是将其从 int[] myList = {100} 更改为 ArrayList myList = new ArrayList()。您可以使用 myList.add(number) 添加数字,并使用 myList.get(i) 查找它们。

Rather than give you the answer outright, here are a couple of hints:

  1. You don't have any loops in BubbleSort().

  2. You should only call BubbleSort() once, after you've read in all the numbers from the file. Meaning, move the call outside of the while loop.

  3. You never increment the variable i so you're just overwriting myList[0] each time through your while loop.

  4. Arrays are not resizable. If you try to assign to myList[1] or myList[2] you will get an ArrayIndexOutOfBoundsException error. There are several ways to solve this--one is to change it from int[] myList = {100} to ArrayList myList = new ArrayList(). You can add numbers to it with myList.add(number) and look them up with myList.get(i).

晨曦÷微暖 2024-08-16 01:51:43

将其更改

 try{
    File myFile = new File("arraySort.txt");
    Scanner scan = new Scanner(myFile);
    while(scan.hasNext()){                
        myList[i] = scan.nextInt();
        BubbleSort(myList);
        System.out.println(myList[i]);
 } 
 catch(IOException e){
     System.out.println("File not found!");
 }

try{
    File myFile = new File("arraySort.txt");
    Scanner scan = new Scanner(myFile);
    while(scan.hasNext()){                
        myList[i] = scan.nextInt();
 } 
 catch(IOException e){
     System.out.println("File not found!");
 }

 BubbleSort(myList);
 System.out.println(myList[i]);

: }

根据 @Federico< 的答案更改排序方法/a>

Change this:

 try{
    File myFile = new File("arraySort.txt");
    Scanner scan = new Scanner(myFile);
    while(scan.hasNext()){                
        myList[i] = scan.nextInt();
        BubbleSort(myList);
        System.out.println(myList[i]);
 } 
 catch(IOException e){
     System.out.println("File not found!");
 }

to:

try{
    File myFile = new File("arraySort.txt");
    Scanner scan = new Scanner(myFile);
    while(scan.hasNext()){                
        myList[i] = scan.nextInt();
 } 
 catch(IOException e){
     System.out.println("File not found!");
 }

 BubbleSort(myList);
 System.out.println(myList[i]);

}

Change sort method according to answer by @Federico

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文