如何修复这个冒泡排序程序?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的程序有几个问题,不仅仅是与排序部分有关。
此行将
myList
定义为大小为 1 的数组,包含单个元素100
。然后,您的主循环是您在此循环中不增加
i
,因此您只是用从文件中读取的任何值覆盖myList
中的单个值。当您的Bubblesort
函数尝试访问myList[i+1]
时,它会抛出ArrayIndexOutOfBoundsException
,因为索引处没有元素i+1
(等于1
,因为您不增加i
)。一般来说,特别是对于初学者来说,使用 ArrayList 比使用常规数组更好。此外,您应该首先填充数组,并且只有在数组包含所有元素后才尝试对其进行排序。最后,将变量设置为本地变量而不是类成员是一个更好的主意。这样一来,您的
main
函数就会类似于然后更改
Bubblesort
以采用ArrayList
,然后您还可以使循环索引i
属于Bubblesort
方法。完成后,您可以致力于使冒泡排序算法发挥作用。请记住要小心数组索引,以免访问数组边界之外。Your program has several problems, not just related to the sorting part.
This line defines
myList
as an array with size 1, containing the single element100
. Then, your main loop isYou do not increase
i
in this loop so you are just overwriting the single value inmyList
with whatever value you read from the file. And when yourBubblesort
function tries to accessmyList[i+1]
, it throws anArrayIndexOutOfBoundsException
because there is no element at indexi+1
(which equals1
, since you don't increasei
).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 yourmain
function something likeAnd then change
Bubblesort
to take anArrayList
, and then you can also make the loop indexi
local to theBubblesort
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.这里没有直接给您答案,而是提供一些提示:
您在
BubbleSort()
中没有任何循环。您应该只在读入文件中的所有数字之后调用
BubbleSort()
一次。意思是,将调用移到while
循环之外。您永远不会增加变量
i
,因此您每次通过while
循环时都会覆盖myList[0]
。数组的大小不可调整。如果您尝试分配给
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:
You don't have any loops in
BubbleSort()
.You should only call
BubbleSort()
once, after you've read in all the numbers from the file. Meaning, move the call outside of thewhile
loop.You never increment the variable
i
so you're just overwritingmyList[0]
each time through yourwhile
loop.Arrays are not resizable. If you try to assign to
myList[1]
ormyList[2]
you will get anArrayIndexOutOfBoundsException
error. There are several ways to solve this--one is to change it fromint[] myList = {100}
toArrayList myList = new ArrayList()
. You can add numbers to it withmyList.add(number)
and look them up withmyList.get(i)
.http://www.leepoint.net/notes-java/data/ arrays/32arraybubblesort.html <- 一些冒泡排序示例;)
http://www.leepoint.net/notes-java/data/arrays/32arraybubblesort.html <- some bubble sort example for you ;)
将其更改
为
: }
根据 @Federico< 的答案更改排序方法/a>
Change this:
to:
}
Change sort method according to answer by @Federico