本文共 1579 字,大约阅读时间需要 5 分钟。
- <pre class="cpp" name="code"><img src="https://img-blog.csdn.net/20171130174554451" alt="">package Kuang;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Scanner;
- public class Test {
- public static void main(String[] args) {
-
- List<Book> bookList = new ArrayList<Book>();
- bookList.add(new Book("英语", 29.99));
- bookList.add(new Book("数学", 50));
- bookList.add(new Book("化学", 30));
- bookList.add(new Book("生物", 30));
- bookList.add(new Book("历史", 30));
- System.out.println("请输入价格:\n");
- Book book = new Book("对比价格", 50);
- System.out.println(book.name + book.price);
- for (int i = 0; i < bookList.size(); i++
-
- {
- if (book.compareTo(bookList.get(i)
- System.out.println((bookList.get(i)
-
-
- }System.out.println("价格相同的:"+(book.price));
-
-
-
-
-
-
-
-
-
- }
-
- }</pre><br>
- package Kuang;public class Book implements Comparable<Book>{String name;Double price;public Book (String nm,double p){name=nm;price=p;}@Overridepublic int compareTo(Book o) {
- <pre></pre>
- <br>
-
转载于:https://www.cnblogs.com/spsglz/p/8017890.html