Comparable
:“可比较的”,(欲参与比较的对象对应的元素类需实现Comparable接口)Collection.sort(List)
。其compareTo()
方法只要一个参数,因为这里只有“你”“我”的关系,没有第三方。Comparator
:“比较器”compare()
方法需要两个参数,调用Collection.sort(List, Compatator)
。class 名称<泛型列表>
LinkedList<String> mylist=new LinkedList<String>();
list.add(E obj);
list.remove(index)
public static sort(List<E>list)
折半查找list是否含有和参数key一样的元素
int binarySearch(List<T>,Tkey,compareTo<T>c)
## 习题补做
实验结果截图
Cone<Circle> coneOne; coneOne = new Cone<Circle>(new
import java.util.*;
public class T1 {
public static void main(String[] args) {
Stack<Integer> stack=new Stack<Integer>();
stack.push(new Integer(3));
stack.push(new Integer(8));
int k=1;
while (k<=10) {
for (int i=1;i<=2;i++) {
Integer F1=stack.pop();
int f1=F1.intValue();
Integer F2=stack.pop();
int f2=F2.intValue();
Integer temp= 2 * f1 + 2 * f2;
System.out.println(""+temp.toString());
stack.push(temp);
stack.push(F2);
k++;
}
}
}
}
import java.util.*;
class CollegeStu implements Comparable {
int english=0;
String name;
CollegeStu(int english,String name) {
this.name=name;
this.english=english;
}
@Override
public int compareTo(Object b) {
CollegeStu stu=(CollegeStu)b;
return (this.english-stu.english);
}
}
public class T2 {
public static void main(String[] args) {
List<CollegeStu> list=new LinkedList<CollegeStu>();
int score []={ 90, 88, 80};
String name []={"张三","李四","王五"};
for (int i=0;i<score.length;i++) {
list.add(new CollegeStu(score[i],name[i]));
}
Iterator<CollegeStu> iter=list.iterator();
TreeSet<CollegeStu> mytree=new TreeSet<CollegeStu>();
while (iter.hasNext()) {
CollegeStu stu=iter.next();
mytree.add(stu);
}
Iterator<CollegeStu> te=mytree.iterator();
while (te.hasNext()) {
CollegeStu stu=te.next();
System.out.println(""+stu.name+" "+stu.english);
}
}
}
import java.util.*;
class UDiscKey implements Comparable {
double key=0;
UDiscKey(double d) {
key=d;
}
@Override
public int compareTo(Object b) {
UDiscKey disc=(UDiscKey)b;
if((this.key-disc.key)==0) {
return -1;
}
else {
return (int) ((this.key - disc.key) * 1000);
}
}
}
class UDisc{
int amount;
double price;
UDisc(int m,double e) {
amount=m;
price=e;
}
}
public class T3 {
public static void main(String args[ ]) {
TreeMap<UDiscKey,UDisc> treemap= new TreeMap<UDiscKey,UDisc>();
int amount[]={8,16,32,64};
double price[]={40,50,64,128};
UDisc UDisc[]=new UDisc[4];
for(int k=0;k<UDisc.length;k++) {
UDisc[k]=new UDisc(amount[k],price[k]);
}
UDiscKey key[]=new UDiscKey[4] ;
for(int k=0;k<key.length;k++) {
key[k]=new UDiscKey(UDisc[k].amount);
}
for(int k=0;k<UDisc.length;k++) {
treemap.put(key[k],UDisc[k]);
}
int number=treemap.size();
Collection<UDisc> collection=treemap.values();
Iterator<UDisc> iter=collection.iterator();
while(iter.hasNext()) {
UDisc disc=iter.next();
System.out.println(""+disc.amount+"G "+disc.price+"元");
}
treemap.clear();
for(int k=0;k<key.length;k++) {
key[k]=new UDiscKey(UDisc[k].price);
}
for(int k=0;k<UDisc.length;k++) {
treemap.put(key[k],UDisc[k]);
}
number=treemap.size();
collection=treemap.values();
iter=collection.iterator();
while(iter.hasNext()) {
UDisc disc=iter.next();
System.out.println(""+disc.amount+"G "+disc.price+"元");
}
}
}
原文:https://www.cnblogs.com/wangyaojia/p/8998311.html