package 泛型2;
import java.util.*;
public class Book implements Comparable {
String name;
double price;
Book(String name,double price){
this.price=price;
this.name=name;
}
public int compareTo(Object obj) {
Book b =(Book)obj;
if(b.price==this.price)
return 1;
else
return 0;
}
import java.util.*;
public class Book implements Comparable {
String name;
double price;
Book(String name,double price){
this.price=price;
this.name=name;
}
public int compareTo(Object obj) {
Book b =(Book)obj;
if(b.price==this.price)
return 1;
else
return 0;
}
}
package 泛型2;
import java.util.*;
public class TestBook {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Book> list =new ArrayList<Book>();
Book bk1=new Book("java基础教程",29);
Book bk2=new Book("C++基础教程",29);
Book bk3=new Book("数据库技术",29);
Book bk4=new Book("数学",23);
list.add(bk1);
list.add(bk2);
list.add(bk3);
list.add(bk4);
Book bk5=new Book("模式匹配",29);
System.out.println("新书《"+bk5.name+"》"+"与下列:");
Iterator<Book> it =list.iterator();
while(it.hasNext()) {
Book b=it.next();
if(b.compareTo(bk5)==1)
System.out.println(b.name);
}
System.out.println("价格相同,具体价格为:"+bk5.price);
}
}