在 Java 中对对象数组进行冒泡排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9949817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bubble sorting an object array in Java
提问by Aiden O'Jester
how do I sort an array of objects? this is my code for sorting the array, I receive a "not a statement" error for: Movie temp = movies[b]; what do i declare the temp variable as if it is to hold the exact value/reference of movies[b]; which could be any of three different object types which are in the same array? I am new to programming so I apologize if i seem to be ignorant; please feel free to correct me or ask questions if I phrased the questions incorrectly.
如何对对象数组进行排序?这是我对数组进行排序的代码,我收到一个“not a statement”错误: Movie temp = movies[b]; 我如何声明临时变量,就好像它要保存电影的确切值/引用[b];哪个可能是同一数组中的三种不同对象类型中的任何一种?我是编程新手,如果我似乎无知,我深表歉意;如果我的问题措辞不正确,请随时纠正我或提出问题。
public static String bubbleSort(Movie[] movies) {
for (int a=1; a<movies.length; a++) {
for(int b=0; b<movies.length - a; b++) {
if (((movies[b].getTitle()).compareTo((movies[b+1].getTitle()))) > 0)
//swap movies[b] with movies[b+1]
Movie temp = movies[b];
movies[b] = movies[b+1];
movies[b+1] = temp;
}
}
}
回答by Dave
When an array is defined as Movie[]
it can only contains objects of type Movie
. So you can only have Movie
s in there. However, to make this general, you should define the type as Object
and the array as Object[]
.
当一个数组被定义为Movie[]
它只能包含类型的对象时Movie
。所以你只能Movie
在那里有s 。但是,为了使其通用,您应该将类型定义为Object
并将数组定义为Object[]
。
However, in your code, you are assuming that you really do have Movie
objects because you're using Movie.getTitle()
. You will not be able to access that from references of Object
. I would recommend having your objects implement Comparable
and using the type Comparable
as the type of the array and your temporary variable.
但是,在您的代码中,您假设您确实拥有Movie
对象,因为您使用的是Movie.getTitle()
. 您将无法从 的引用访问它Object
。我建议让您的对象实现Comparable
并使用该类型Comparable
作为数组和临时变量的类型。
回答by Jim Garrison
The Movie temp = movies[b];
is a declaration, not a statement. You want this:
该Movie temp = movies[b];
是一个声明,而不是一个声明。你要这个:
if (((movies[b].getTitle()).compareTo((movies[b+1].getTitle()))) > 0)
{
//swap movies[b] with movies[b+1]
Movie temp = movies[b];
movies[b] = movies[b+1];
movies[b+1] = temp;
}
Note all I did was add braces around all the swap code, making it into a block which cancontain a declaration. I think this is what you intended, but just omitted the braces.
请注意,我所做的只是在所有交换代码周围添加大括号,使其成为一个可以包含声明的块。我认为这就是您的意图,但只是省略了大括号。
回答by Janek Bogucki
You were missing a { after the test, and incorrectly promising to return a String. Try this,
您在测试后遗漏了一个 {,并且错误地承诺返回一个字符串。试试这个,
public static void bubbleSort(Movie[] movies) {
for (int a = 1; a < movies.length; a++) {
for (int b = 0; b < movies.length - a; b++) {
if (((movies[b].getTitle())
.compareTo((movies[b + 1].getTitle()))) > 0) {
// swap movies[b] with movies[b+1]
Movie temp = movies[b];
movies[b] = movies[b + 1];
movies[b + 1] = temp;
}
}
}
}
回答by Manfred Moser
Use the Collections api and dont reinvent the wheel doing your own sorting implementation.
使用 Collections api,不要重新发明轮子做你自己的排序实现。