gpt4 book ai didi

java - Hashtable myhash = new Hashtable();

转载 作者:行者123 更新时间:2023-12-01 07:07:18 27 4
gpt4 key购买 nike

我想知道是否可以这样:

Hashtable<String,Integer[]> myhash =new Hashtable();

其中 String 是一个单词,整数[]是一个包含两个位置的数组,第一个位置是行号,第二个位置是该单词出现的次数。

但我真的不知道如何进行 put 操作。

myhash.put(word, new Integer(n_line,n_times));??

这不起作用,但你会怎么做呢?

谢谢

最佳答案

要将新的整数数组放入表中,请使用 myhash.put(word, new Integer[]{ n_line,n_times});

但是,我建议为该值创建一个新对象,例如

//Note that I ommitted stuff like modifiers, constructors etc. for simplicity's sake
//In most cases you'd want to expand that class and add what's missing (left to you as an excercise)
class WordCounter {
int lines;
int time;
}

Hashtable<String,WordCounter > myhash =new Hashtable<>();

作为旁注,请记住 HashtableHashMap 慢,因为它同步访问,因此除非您需要确切的行为,否则您可以尝试使用 HashMapConcurrentHashMap 代替。

关于java - Hashtable<String,Integer[]> myhash = new Hashtable();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21237036/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com