按字母顺序排序的Java,看起来应该可以工作,但不是

时间:2014-06-19 23:59:21

标签: java

我在我的冒泡排序方程中有这个计算,但它似乎没有工作,我可以找出原因。

这是我的排序课。

System.out.println("\r" + "In order by title");
for (int out = 0; out < bookList.size(); out++) {
    for (int in = 0; in < bookList.size() - 1; in++) 
        if (bookList.get(in).getTitle().compareTo(bookList.get(in + 1).getTitle()) < 0) {
            Book temp = bookList.get(in);
            bookList.set(in, bookList.get(in+1));
            bookList.set(in+1, temp);        
        }    

        System.out.println(bookList.get(out).getTitle() + "   " + bookList.get(out).getRating());
}
}

线......

 if (bookList.get(in).getTitle().compareTo(bookList.get(in + 1).getTitle()) < 0) {

是我相信我的问题所在。我似乎无法让我的程序按字母顺序对书籍列表进行排序。有人知道解决方案吗?我的其余代码如下。

课程:预订

public class Book {
String title;
int rating;

public Book(String pTitle, int pRating) {
    title = pTitle;
    rating = pRating;
}

public String getTitle() {
    return title;
}

public int getRating() {
    return rating;
}

public void setTitle(String newTitle) {
    title = newTitle;
}

public void setRating(int newRating) {
    rating = newRating;
}    
}

课程:图书馆

import java.util.ArrayList;

public class Library {

public static void main (String [] args) {

    ArrayList<Book> bookList = new ArrayList<Book>();

    Book b1 = new Book ("Huckleberry Finn", 5);
    Book b2 = new Book ("The Great Gadsby", 2);
    Book b3 = new Book ("Harry Potter", 3);
    Book b4 = new Book ("Animal Farm", 4);
    Book b5 = new Book ("The Mist", 1);
    bookList.add(b1);
    bookList.add(b2);
    bookList.add(b3);
    bookList.add(b4);
    bookList.add(b5);
    System.out.println("Original sequence");

    for (int cnt = 0; cnt < videoList.size(); cnt++) {
        System.out.println(bookList.get(cnt).getTitle() + "   " + bookList.get(cnt).getRating());

 Sort sortObject = new Sort();
 sortObject.calc(bookList);

    }
}
}   

2 个答案:

答案 0 :(得分:0)

您可以通过实现Comparable和compareTo方法轻松对Collection进行排序,然后使用Collections.sort(arraylist);

例如:

public class Book implements Comparable<Book>
{
   String title;
    ......

   public int compareTo(Book other)
   {
      return title.compareTo(other.title);
   }
}

现在你可以通过以下方式对你的arraylist进行排序:Collections.sort(bookList);

答案 1 :(得分:0)

在以下情况后您缺少一个括号:

for (int in = 0; in < bookList.size() - 1; in++) 

另外,如果您打算打印出有序列表,您只需放错了这一行:

System.out.println(bookList.get(out).getTitle() + "   " + bookList.get(out).getRating());

您的代码应该是这样的:

System.out.println("\r" + "In order by title");
for (int out = 0; out < bookList.size(); out++) {
    for (int in = 0; in < bookList.size() - 1; in++) {
        if (bookList.get(in).getTitle().compareTo(bookList.get(in + 1).getTitle()) < 0) {
            Book temp = bookList.get(in);
            bookList.set(in, bookList.get(in+1));
            bookList.set(in+1, temp);        
        }
    }
}
System.out.println("Ordered list of books:");
for (int i = 0; out < bookList.size(); i++) {
    System.out.println(bookList.get(out).getTitle() + "   " + bookList.get(out).getRating());
}

无论如何,你应该真正学会如何实现和使用Comparable。它将使您的未来生活更轻松。