[求助]TreeMap泛型类
这是TreeMap泛型类的用法示例如下所示的程序可以运行
但是我想让结构更清晰,于是尝试把红色部分的匿名对象(实现了Comparator<E>泛型接口的对象)去掉,重新在构造方法外定义(见蓝色部分),生成一个对象传给TreeMap<K,V>的构造方法.但是编译提示出错.(见紫色)请问是什么地方出了问题?是不是我的返型接口使用方法错了?
import java.util.*;
class Disk
{
int Price;
int Size;
Disk(int Price,int Size)
{
this.Price=Price;
this.Size=Size;
}
}
class Key_ implements Comparable
{
int number=0;
Key_(int number)
{
this.number=number;
}
public int compareTo(Object o)
{
Key_ key=(Key_)o;
if(this.number==key.number)
return 1;
else
return this.number-key.number;
}
}
//class Mycom<T> implements Comparator<T>//本想把TreeMap的参数单独出来定义,可是编译出错
//{
// public int compare(T a,T b)
// {
// return a.compareTo(b);//本想把TreeMap的参数单独出来定义,可是编译出错:找不到符号
// }
//}
class Main
{
public static void main(String[] args)
{
Disk d1=new Disk(100,4000),
d2=new Disk(200,3000),
d3=new Disk(300,2000),
d4=new Disk(400,1000);
// Mycom<Key_> key=new Mycom<Key_>();
TreeMap<Key_,Disk> tree=new TreeMap<Key_,Disk>(
// key
new Comparator<Key_>()
{public int compare(Key_ a,Key_ b)
{
return a.compareTo(b);
}
}
);
tree.put(new Key_(d1.Price),d1);
tree.put(new Key_(d2.Price),d2);
tree.put(new Key_(d3.Price),d3);
tree.put(new Key_(d4.Price),d4);
Collection<Disk> collection=tree.values();
Iterator<Disk> iter=collection.iterator();
while(iter.hasNext())
{
Disk te=iter.next();
System.out.printf("价钱%d,容量%d\n",te.Price,te.Size);
}
//////////////
tree.clear();
tree.put(new Key_(d1.Size),d1);
tree.put(new Key_(d2.Size),d2);
tree.put(new Key_(d3.Size),d3);
tree.put(new Key_(d4.Size),d4);
Collection<Disk> collection2=tree.values();
Iterator<Disk> iter2=collection.iterator();
while(iter2.hasNext())
{
Disk te=iter2.next();
System.out.printf("容量%d,价钱%d\n",te.Size,te.Price);
}
}
}
class Disk
{
int Price;
int Size;
Disk(int Price,int Size)
{
this.Price=Price;
this.Size=Size;
}
}
class Key_ implements Comparable
{
int number=0;
Key_(int number)
{
this.number=number;
}
public int compareTo(Object o)
{
Key_ key=(Key_)o;
if(this.number==key.number)
return 1;
else
return this.number-key.number;
}
}
//class Mycom<T> implements Comparator<T>//本想把TreeMap的参数单独出来定义,可是编译出错
//{
// public int compare(T a,T b)
// {
// return a.compareTo(b);//本想把TreeMap的参数单独出来定义,可是编译出错:找不到符号
// }
//}
class Main
{
public static void main(String[] args)
{
Disk d1=new Disk(100,4000),
d2=new Disk(200,3000),
d3=new Disk(300,2000),
d4=new Disk(400,1000);
// Mycom<Key_> key=new Mycom<Key_>();
TreeMap<Key_,Disk> tree=new TreeMap<Key_,Disk>(
// key
new Comparator<Key_>()
{public int compare(Key_ a,Key_ b)
{
return a.compareTo(b);
}
}
);
tree.put(new Key_(d1.Price),d1);
tree.put(new Key_(d2.Price),d2);
tree.put(new Key_(d3.Price),d3);
tree.put(new Key_(d4.Price),d4);
Collection<Disk> collection=tree.values();
Iterator<Disk> iter=collection.iterator();
while(iter.hasNext())
{
Disk te=iter.next();
System.out.printf("价钱%d,容量%d\n",te.Price,te.Size);
}
//////////////
tree.clear();
tree.put(new Key_(d1.Size),d1);
tree.put(new Key_(d2.Size),d2);
tree.put(new Key_(d3.Size),d3);
tree.put(new Key_(d4.Size),d4);
Collection<Disk> collection2=tree.values();
Iterator<Disk> iter2=collection.iterator();
while(iter2.hasNext())
{
Disk te=iter2.next();
System.out.printf("容量%d,价钱%d\n",te.Size,te.Price);
}
}
}
[此贴子已经被作者于2007-7-28 14:36:35编辑过]