这是我试图打印密钥的代码,但它永远不会被执行,它只是转到最后一行并引发空指针异常。
Enumeration<String> keys = states.keys();
int max=0;
String maxClass="";
while(keys.hasMoreElements())
{
String key = keys.nextElement();
System.out.println(key);
if(states.get(key)>=max)
{
max=states.get(key);
maxClass=key;
}
System.out.println(maxClass);
}
System.out.println(maxClass);
Category c = classes.get(maxClass);
c.add(p);
这是分类功能的完整代码。
static void classify(ArrayList<Point> cities,Hashtable<String,Category> classes)
{
for(Point p: cities)
{
ArrayList<Point> nbours = new ArrayList<Point>();
nbours.addAll(getNbours( p,cities));
// System.out.println(p.city);
Hashtable<String,Integer> states = new Hashtable<String,Integer>();
for(Point pt : nbours)
{
//System.out.println(pt.city);
if(states.containsKey(pt.state))
{
int cnt = states.get(pt.state) + 1;
states.remove(pt.state);
states.put(pt.state,cnt);
//states.add(state,);
}
else
{
states.put(pt.state,1);
}
//System.out.println(states.get(pt.state));
}
Enumeration<String> keys = states.keys();
int max=0;
String maxClass="";
System.out.println(keys);
while(keys.hasMoreElements())
{
String key = keys.nextElement();
System.out.println(key);
if(states.get(key)>=max)
{
max=states.get(key);
maxClass=key;
}
System.out.println(maxClass);
}
System.out.println(maxClass);
Category c = classes.get(maxClass);
c.add(p);
System.out.println(p.city+ "Classified");
nbours.clear();
}