利用List容器通过冒泡实现对 LIst容器中的Book类进行排序

在写之前突然发现冒泡算法差点搞完了 …

package List;

import java.util.*;

public class List02 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(new Book("红楼梦",100,"曹雪芹"));
        list.add(new Book("西游记",10,"吴承恩"));
        list.add(new Book("水浒传",9,"施耐庵"));
        list.add(new Book("三国演义",80,"罗贯中"));
        list.add(new Book("西游记",10,"吴承恩"));

        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list.size() - i -1; j++) {
                Book book = (Book)list.get(j);
                Book book01 = (Book)list.get(j+1);
                if(book.getPrice() > book01.getPrice()){
                    Book b = (Book) list.get(j);
                    list.set(j,list.get(j+1));
                    list.set(j+1,b);
                }
            }
        }

        Iterator iterator = list.iterator();

        while (iterator.hasNext()) {
            Object next =  iterator.next();
            System.out.println(" Book :" + next);
        }

    }
}

class Book {
    private String name;
    private double price;
    private String author;

    public Book(String name, double price, String author) {
        this.name = name;
        this.price = price;
        this.author = author;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

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

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", author='" + author + '\'' +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值