日期:2014-05-20 浏览次数:20692 次
import java.util.HashMap;
import java.util.StringTokenizer;
public class HashMapSample {
HashMap<String, String[]> dict;
public HashMapSample() {
dict = new HashMap<String, String[]>();
}
public void putIntoDict( String key, String info ) {
StringTokenizer st = new StringTokenizer(info, "\n");
int length = st.countTokens();
String [] result = new String[length];
for (int i = 0; i < length; i++) {
result[i] = st.nextToken();
}
dict.put( key, result );
}
public void printWord( Object key ) {
System.out.println( key + ":" );
for( int j = 0; j < ( (String [])(dict.get(key)).length ); j++ ) {//这一行类型转换有错误,求助高手帮助。
System.out.println( ((String [])dict.get(key))[j] );
}
}
public void printDict() {
Object [] keys = dict.keySet().toArray();
for( int i = 0; i < keys.length; i++ ) {
printWord( keys[i] );
}
}
public static void main(String[] args) {
HashMapSample map = new HashMapSample();
String str = "adj : appearing earlier in the same text; \"flaws in the above interpretation\"\n" +
"adv 1: at an earlier place; \"see above\"\n"+
"2: in or to a place that is higher";
map.putIntoDict( "above", str );
map.printDict();
return;
}
}
for( int j = 0; j < dict.get(key).length ; j++ ) ////这一行类型转换有错误,求助高手帮助。dict.get(key)本身就是String[].不需要强转了。
{
System.out.println(dict.get(key)[j] );
}