简体   繁体   English

HashMap不起作用

[英]HashMap does not work

Nothing happens when I use the method "frageStellen" - why? 当我使用“ frageStellen”方法时什么也没发生-为什么? I want my computer to return the answer from the HashMap "ratschlagMap" which fits to "frage". 我希望我的计算机从适合“ frage”的HashMap“ ratschlagMap”返回答案。

import java.util.HashMap;

public class Kapitän
{
   private HashMap<String, String> ratschlagMap;

public Kapitän()
{
 ratschlagMap = new HashMap<String, String>();
 antwortenGenerieren();  
}

public void antwortenGenerieren()
{
    ratschlagMap.put("hallo", "Guten Tag, mein Herr!");
    ratschlagMap.put("wetter", "Es riecht nach Sturm!");
    ratschlagMap.put("liebe", "Die Liebe ist wie die See: erst stürmisch, dann flaut sie ab...");
    ratschlagMap.put("zukunft", "Erst die Segel setzten, dann übers Ziel nachdenken!");
}

public void frageStellen (String frage)
{
    frage.toLowerCase();

    if (ratschlagMap.containsKey(frage))
    {
       ratschlagMap.get(frage);
    }
}
}

Thanks!! 谢谢!!

If you are using hashmap.get you probably want to return this value. 如果您使用的是hashmap.get,则可能要返回此值。 Also you don't need to check if map contains this element - it will return null if no value will be found. 同样,您也不需要检查map是否包含此元素-如果找不到值,它将返回null。 Take a look at this code: 看一下这段代码:

public String frageStellen (String frage)
{ 
    return ratschlagMap.get(frage.toLowerCase());
}

You also probably should read more about Java language... 您可能还应该阅读更多有关Java语言的信息。

There are two problems with this code. 此代码有两个问题。 The first one iswith this line: 第一个与此行:

 frage.toLowerCase();

This method returns a string, it doesn't modify the string in place (since String objects in Java are immutable). 此方法返回一个字符串,它不会就地修改该字符串(因为Java中的String对象是不可变的)。 You can just use frage.toLowerCase() as the key. 您可以只使用frage.toLowerCase()作为键。

Then you have this line: 然后你有这行:

ratschlagMap.get(frage);

This method returns the value for the key frage . 此方法返回关键frage的值。 But you don't do anything with this value, not even assign it to a variable. 但是您不会对此值做任何事情,甚至不会将其分配给变量。 This is a bug. 这是一个错误。

I could be wrong but in youre method "frageStellen" you only do ratschlagMap.get(frage); 我可能是错的,但是在您的方法“ frageStellen”中,您只会做ratschlaglag.get(frage); but you dont save it into a variable or return it. 但您不要将其保存到变量中或将其返回。

There's a couple of issues. 有几个问题。

First, String is immutable - any operations on it will create a new String instance. 首先,String是不可变的-对它的任何操作都将创建一个新的String实例。 If you don't want it around, then you could only really create it on the fly, where you need it: 如果您不想要它,那么您只能在需要的地方即时创建它:

 if (ratschlagMap.containsKey(frage.toLowerCase()))

Second, you're not returning anything - you don't declare your method to do so. 其次,您没有返回任何内容-您没有声明要这样做的方法。 The thing about a Map#get() is that it will return null if the value doesn't exist at the key passed in, which seems reasonable in your case. 关于Map#get()的事情是,如果传入的键中的值不存在,则它将返回null ,这在您的情况下似乎是合理的。

That means it'd be more straightforward to write this, since we don't need to really check for existence: 这意味着编写此代码会更加直接,因为我们不需要真正检查是否存在:

public String frageStellen(String frage) {
    return ratschlagMap.get(frage.toLowerCase());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM