简体   繁体   English

哈希表内容:前两个键为Null ...如何获取我保存的内容

[英]Hashtable content : the first 2 Keys are Null … How to get my saved content

I have a problem with my hashtable... I have a hashtable< String1,String2 > String1 = is a JTextfield as my Hashtable Key String2 = is a JTextArea as my Hashtable content myHashtable.put(JTextfield.getText(),JTextArea.getText()); 我的哈希表有问题...我有一个哈希表<String1,String2> String1 =是一个JTextfield作为我的哈希表键String2 =是一个JTextArea作为我的哈希表内容myHashtable.put(JTextfield.getText(),JTextArea.getText()); and now I want to write all my saved content from my Hashtable into a file but my first 2 contents are "null" 现在我想将我的哈希表中所有保存的内容写到文件中,但我的前2个内容为“空”

for (int i = 0; i < myHashtable.size(); i++) {
            String[] key = myHashtable.keySet().toString().split(", ");
            writer.println(key[i].replace("]", "").replace("[", "") + ": "
                    + myHashtable.get(key[i]));

the following output should looks like: 以下输出应类似于:

examplekey : some content examplekey:一些内容
examplekey2 : more content examplekey2:更多内容
examplekey3 : some content examplekey3:一些内容
but it looks like: 但它看起来像:

examplekey : null examplekey:null
examplekey2 : null examplekey2:空
examplekey3 : some content examplekey3:一些内容

the reason why I write it so, is that I want to read this file, to get my Hashtable content after a restart and the .keySet function gives me a "[" and "]" at the start and end thats why I replace this with "". 之所以这样写,是因为我想读取此文件,以在重新启动后获取我的Hashtable内容,并且.keySet函数在开始和结束时给我一个“ [”和“]”,这就是我替换该文件的原因与“”。

You should run your program through a debugger to better understand what it is doing. 您应该通过调试器运行程序,以更好地了解其功能。

Let's follow an example: 让我们来看一个例子:

Hashtable<String, String> myHashtable = new Hashtable<>();
myHashtable.put("1", "rat");
myHashtable.put("2", "cat");
myHashtable.put("3", "bat");

Running your loop, the first iteration will go like this: 运行循环,第一次迭代将如下所示:

myHashtable.keySet().toString() -> "[3, 2, 1]"

String[] key = myHashtable.keySet().toString().split(", ") -> "[3", "2", "1]"

key[0] -> "[3"
key[0].replace("]", "").replace("[", "") -> "3"
myHashtable.get(key[0]) -> myHashtable.get("[3") -> null

As you can see, replace() returns a new string with the replacement in place of the target. 如您所见, replace()返回一个新字符串,其中替换的字符串代替了目标字符串。 It does not modify the original string. 它不会修改原始字符串。 So when you call myHashtable.get(key[0]) the key is "[3" , which is not in the hashtable, so it returns null . 因此,当您调用myHashtable.get(key[0]) ,键为"[3" ,该键不在哈希表中,因此它返回null If you run your code with the example hashtable you will get something like this in your file: 如果使用示例哈希表运行代码,则文件中将得到以下内容:

3: null
2: cat
1: null

It works for the keys in the middle, "2" in the example, because when you split the string they already don't have a "[" or "]" attached to them. 它适用于中间的键,在示例中为"2" ,因为在拆分字符串时,它们已经没有附加"[""]"键。

Now, notice that when you do myHashtable.keySet().toString().split(", ") you already have access to the elements in the keyset. 现在,请注意,当您执行myHashtable.keySet().toString().split(", ")您已经可以访问键集中的元素。 You don't have to convert it to a string and then try to get the keys from this string. 您不必将其转换为字符串,然后尝试从该字符串获取密钥。 So you can do: 因此,您可以执行以下操作:

for (String key : myHashtable.keySet()) {
    writer.println(key + ": " + myHashtable.get(key));
}

We can make it a bit faster if we iterate over all entries directly: 如果直接迭代所有条目,我们可以使其速度更快:

for (Map.Entry<String, String> entry : myHashtable.entrySet()) {
    writer.out.println(entry.getKey() + ": " + entry.getValue());
}

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

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