我的arrayList不会逐个横切列表并打印所有内容?

时间:2016-01-11 13:42:12

标签: java sorting arraylist

我在类returnListOfBooks中有一个单独的方法BookShelf,按字母顺序按标题排序

private ArrayList<Book> listOfBooks;

    public BookShelf(ArrayList<Book> listOfBooks) {
        this.listOfBooks = listOfBooks;
    }

    public void addBook(Book a) {
        listOfBooks.add(a);
    }

public ArrayList<Book> returnListOfBooks() {
        for (int i = listOfBooks.size() - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                //Is the two elements out of order?
                if (listOfBooks.get(j).getTitle().compareTo(
                        listOfBooks.get(j + 1).getTitle()) < 0) 
                {
                    //yes, then swap
                    Book temp = listOfBooks.get(j);
                    listOfBooks.set(j, listOfBooks.get(j + 1));
                    listOfBooks.set(j + 1, temp);

                } // end of inner for loop - arranging

            }
        }        
        for (int t = 0; t<listOfBooks.size(); t++)
        {
            System.out.print(listOfBooks.get(t) + " ");    
        }

        System.out.println();
        return listOfBooks;
    }

用户必须在书架上输入20本书,一旦已有20本书,我必须在主驱动程序中调用此方法并遍历书籍列表并逐个打印按字母顺序排序订购。但是,当我这样做时,仅打印出排序列表中的第一本书详细信息。

这是我在主驱动程序中调用该方法的方法:

ArrayList<Book> listOfBooks = myBookshelf.returnListOfBooks();
        for (Book b: listOfBooks){
            System.out.println(b);
        }

任何人都知道我哪里出错了?希望你能帮助我。

  

示例输入:
  输入图书的ID:23456

     

输入该书的作者:J.K Rowling

     

输入书名:哈利波特和火焰杯

     

输入图书的ID:56780

     

输入该书的作者:John Green

     

输入书名:我们开始时的错误

     

输入图书的ID:45678

     

输入该书的作者:James Patterson

     

输入书名:Zoo

     

...

     

(用户将输入20次)

     

预期产出:

     

ID:23456,作者:J.K罗琳,标题:哈利波特与高脚杯   火

     

ID:56780,作者:John Green,标题:我们开始的错误

     

ID:45678,作者:James Patterson,标题:动物园

     

...

     

(依此类推,按字母顺序按标题排列)

我得到的输出:

Enter ID of the book: 23456

Enter the author of the book: J.K Rowling

Enter title of the book: Harry Potter and the Goblet of Fire

Enter ID of the book: 56780

Enter the author of the book: John Green

Enter title of the book: The Fault in Our Starts

Enter ID of the book: 45678

Enter the author of the book: James Patterson

Enter title of the book: Zoo

...

(输入20次)

ID: 23456, Author: J.K Rowling, Title: Harry Potter and the Goblet of Fire
    BUILD SUCCESSFUL (total time: 16 seconds)

1 个答案:

答案 0 :(得分:0)

你没有提供所有必要的代码,所以我制作了我的版本。也许它会有所帮助。取消注释代码以便能够从控制台输入数据,或者只是向阵列添加更多数据。

import java.io.Console;
import java.util.*;
class Book
{
    private int id;

    private String author;
    private String title;

    public Book() {}

    public Book(int id, String author, String title)
    {
        this.id = id;
        this.author = author;
        this.title = title;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle()
    {
        return this.title;
    }

    @Override
    public String toString()
    {
        return "ID: " + this.id + ", Author: " + this.author + ", Title: " + this.title;
    }
}

public class BookShelf
{
private List<Book> listOfBooks;

public BookShelf(List<Book> listOfBooks) {
    this.listOfBooks = listOfBooks;
}

public void addBook(Book a) {
    listOfBooks.add(a);
}

public List<Book> returnListOfBooks()
{
    Collections.sort(listOfBooks, this.cmp);
    return listOfBooks;
}

Comparator<Book> cmp = new Comparator<Book>()
{
    @Override
    public int compare(Book o1, Book o2)
    {
        int cmpresult = o1.getTitle().compareTo(o2.getTitle());
        return cmpresult;
    }
};

public static void main(String[] args)
{
    List<Book> list = new ArrayList<Book>();

    /*
    Console con = System.console();
    if(con == null)
        System.exit(0);

    for(int i = 0; i < 3; i++ )
    {
        Book b = new Book();
        System.out.print("Enter ID of the book: ");
        b.setId(Integer.parseInt(con.readLine()));
        System.out.print("Enter the author of the book: ");
        b.setAuthor(con.readLine());
        System.out.print("Enter title of the book: ");
        b.setTitle(con.readLine());
        list.add(b);

    }
    */
    list = Arrays.asList(new Book[]{new Book(45678, "James Patterson", "Zoo"),
            new Book(56780, "John Green", "The Fault in Our Starts"),
            new Book(23456, "J.K Rowling", "Harry Potter and the Goblet of Fire")
    });

    BookShelf bs = new BookShelf(list);
    for(Book b : list)
    {
        System.out.println(b);
    }

    List<Book> list2 = bs.returnListOfBooks();
    for(Book b : list2)
    {
        System.out.println(b);
    }
  }
}