- <pre class="cpp" name="code"><img src="http://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) {
- // TODO Auto-generated method stub
- 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("请输入价格: ");
- Book book = new Book("对比价格", 50);
- System.out.println(book.name + book.price);
- for (int i = 0; i < bookList.size(); i++
- /* Book books:bookList)*/)
- {
- if (book.compareTo(bookList.get(i)/*books*/) == 0)
- System.out.println((bookList.get(i)/*books*/.name));
- //+ (bookList.get(i)/*books*/.price)));
- }System.out.println("价格相同的:"+(book.price));
- // Map<Book> bookmap = new HashMap<Book>();
- // bookmap.put("english","18.5");
- // bookmap.put("math","18.5");
- // bookmap.put("chinese","18");
- // bookmap.put("生物","185");
- // bookmap.put("化学","15");
- // bookmap.put("历史","8.5");
- // System.out.println("请输入您要搜索的价格: ");
- // Scanner input = new Scanner(System.in);
- }
- }</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) {// TODO Auto-generated method stubreturn this.price.compareTo(o.price);}}
- <pre></pre>
- <br>