public class ReadToHashmap {
public static void main(String[] args) throws Exception {
Map<String, String> map = new HashMap<String, String>();
BufferedReader in = new BufferedReader(new FileReader("example.tab"));
String line = "";
while ((line = in.readLine()) != null) {
String parts[] = line.split("\t");
map.put(parts[0], parts[1]);
}
in.close();
System.out.println(map.toString());
}
}
同時插入與價值的關鍵在HashMap
我在得到ArrayIndexOutOfBoundException
例外: -ArrayIndexOutOfBoundException在HashMap中
map.put(parts[0], parts[1]);
你似乎沒有被檢查假定它有至少前'parts'數組中元素的個數兩個要素。 –
問題標題誤導爲'ArrayIndexOutOfBoundException'不是由'HashMap'的任何方法拋出,而是由字符串數組''[''''在某些情況下其長度小於2「)拋出。 – ares