Java 我的arrayList赢了';不要一个接一个地横排列表,然后打印所有内容?
我有一个单独的方法Java 我的arrayList赢了';不要一个接一个地横排列表,然后打印所有内容?,java,sorting,arraylist,Java,Sorting,Arraylist,我有一个单独的方法returnListOfBooks在一个类BookShelf中,它按标题按字母顺序排序 private ArrayList<Book> listOfBooks; public BookShelf(ArrayList<Book> listOfBooks) { this.listOfBooks = listOfBooks; } public void addBook(Book a) { listOf
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次)
你没有提供所有必要的代码,所以我制作了我的版本。也许会有帮助。取消注释代码,以便能够从控制台输入数据,或者只是向数组添加更多数据
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);
}
}
}
导入java.io.Console;
导入java.util.*;
课堂用书
{
私有int-id;
私有字符串作者;
私有字符串标题;
公共图书(){}
公共图书(int-id、字符串作者、字符串标题)
{
this.id=id;
this.author=作者;
this.title=标题;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getAuthor(){
返回作者;
}
公共void setAuthor(字符串编写器){
this.author=作者;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共字符串getTitle()
{
返回此.title;
}
@凌驾
公共字符串toString()
{
返回ID:+this.ID+,Author:+this.Author+,Title:+this.Title;
}
}
公共类书架
{
私人图书目录;
公共书架(图书目录){
this.listOfBooks=listOfBooks;
}
公共作废地址簿(a册){
增加(a);
}
公共列表返回图书列表()
{
Collections.sort(图书列表,this.cmp);
返回图书目录;
}
比较器cmp=新比较器()
{
@凌驾
公共整数比较(o1册、o2册)
{
int cmpresult=o1.getTitle().compareTo(o2.getTitle());
返回cmpreslt;
}
};
公共静态void main(字符串[]args)
{
列表=新的ArrayList();
/*
Console con=System.Console();
如果(con==null)
系统出口(0);
对于(int i=0;i<3;i++)
{
书b=新书();
System.out.print(“输入图书ID:”);
b、 setId(Integer.parseInt(con.readLine());
System.out.print(“输入书的作者:”);
b、 setAuthor(con.readLine());
系统输出打印(“输入书名:”);
b、 setTitle(con.readLine());
列表.添加(b);
}
*/
list=Arrays.asList(新书[]{新书(45678,“詹姆斯·帕特森”,“动物园”),
新书(56780,“约翰·格林”,“我们起步的失误”),
新书(23456,“J.K.罗琳”,“哈利波特与火焰杯”)
});
书架bs=新书架(列表);
(b册:目录)
{
系统输出打印ln(b);
}
List list2=bs.returnListOfBooks();
(b册:清单2)
{
系统输出打印ln(b);
}
}
}
您没有显示如何初始化图书列表
(您在几个小时前发布的同一问题中也没有显示)。看起来数组中只有一本书。你是如何阅读这些书的?是否使用add()?或set()?请将系统添加到:public ArrayList returnListOfBooks(){system.output.println(listOfBooks);@Eran对不起。添加了in@Arkadiy已使用add()
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
ID: 23456, Author: J.K Rowling, Title: Harry Potter and the Goblet of Fire
BUILD SUCCESSFUL (total time: 16 seconds)
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);
}
}
}