[英]Adding values from a text file into a HashMap
我需要将包含大量数据的文本文件中的值添加到 HashMap 中。 这是来自文本文件内容的一个小示例:
Outlook 温度 湿度 有风 GoOutside
阳光明媚.......热......高.......假......没有
阴……热…………高…………假……是的
“.....”是文本文件中的空格。
我已经将每列的标题存储在一个数组中,并且我希望它们成为我的 HashMap 中的键,例如
array[0] == "outlook"
我有以下 HashMap
HashMap<String, String> map = new HashMap<String, String>();
假设程序正确读取文件,我如何确保将每个值插入到正确的键中?
感谢您的时间
使用 HashMap inbuit 函数 put 将键插入到 HashMap 中,并使用 get 函数获取插入到 HashMap 中的任何特定键的值。
HashMap<String,String[]> map = new HashMap<>();
//write code for taking input from file into string arrays
for(int i = 0; i < numOfRows; i++){
for(int j = 0; j < 4; j++){
map.put(weather[i], condition[i][j]);
}
}
这里 i 是每个天气的输入行数,j 是以下字符串值,即温度、湿度、有风和外出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.