- 数(三声)数(四声)问题自然使用非负整数;
0. 一个类作为一个计数器
class Counter {
private static long counter;
private final long id = counter ++;
@Override
public String toString() {
return getClass().getSimpleName()+" " + id;
}
}
1. 使用 map
public class StringCounter extends HashMap<String, Integer>{
public void count(String name) {
Integer quantity = get(name);
if (quantity == null) {
put(name, 1);
} else {
put (name, quantity + 1);
}
}
}