我正在使用hashmap,但它无法正常工作.我有一个Computing.java类,我有添加到hashmap的方法:
addIntoMap(String key, String value){
m_parameters_values.put(key, value);
}
Run Code Online (Sandbox Code Playgroud)
和从hashmap获取的方法:
public void getValueByKey(String key){
System.out.println("GET "+m_parameters_values.get(key));
}
Run Code Online (Sandbox Code Playgroud)
我从Main类调用theese方法,但我无法获取密钥,我仍然无效.你知道为什么吗?
类计算的内容(构造函数省略):
public void Parse (String args[]) throws Exception{
Parse(args,true);
}
public void Parse(String[] args, boolean throwErrorIfParamenterNotDefined) throws CmdLineException
{
int i = 0;
while (i < args.length)
{
// The current string is a parameter name
String key = args[i].substring(1, args[i].length() - 1).toLowerCase();
String value = "";
i++;
if (i < args.length)
{
if (args[i].length() > 0 && args[i] == "-")
{
// The next string is a new parameter, do not nothing
} else
{
// The next string is a value, read the value and move forward
value = args[i];
i++;
}
}
if (!m_parameters.containsKey(key))
{
if (throwErrorIfParamenterNotDefined)
{
//throw new CmdLineException("Parameter is not allowed.");
}
//continue;
}
System.out.println("Key: "+key+" value: "+value);
m_parameters_values.put(key, value);
//System.out.println("GET "+m_parameters_values.get("provider"));
}
// Check that required parameters are present in the command line.
for (String key : m_parameters.keySet())
{
if (m_parameters.get(key).required() && !m_parameters.get(key).exists())
throw new CmdLineException("Required parameter is not found.");
}
}
public void getValueByKey(String key){
System.out.println("GET "+m_parameters_values.get(key));
}
Run Code Online (Sandbox Code Playgroud)
主要课程内容:
public static void main(String[] args) {
Computing comp = new Computing("Computing");
cmdLine.Parse(args);
cmdLine.getValueByKey("convert");
Run Code Online (Sandbox Code Playgroud)
方法中value使用的印刷品put.
加.到javadoc:
返回值null不一定表示映射不包含键的映射; 地图也可能将键明确映射为null.
可以肯定的是,使用containsKey方法来验证是否key确实存在.
使用以下代码getValueByKey; 并看看它是否打印任何东西:
if (m_parameters.containsKey(key))
{
System.out.println("GET "+m_parameters_values.get(key));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7517 次 |
| 最近记录: |