将数组中的单词放入HashMap和HashSet中 [英] Putting words from an array into a HashMap and a HashSet
问题描述
我在blueJ中编写代码,我试图做的是这样的:
1.a)创建一个<$ c $其中:
- 创建
a HashSet< String> $ c $
- 使用两个for循环将来自this的所有单词和参数
WordGroup
放入HashSet
- 返回
HashSet< String>
- 使用两个for循环将来自this的所有单词和参数
中的c> getWordSet()> WordGroup li>以另一个
> WordGroup
对象作为参数 1.b)在 到目前为止我的代码: WordGroup类: 我被卡住了。我无法弄清楚如何从数组中获取单词到哈希集和散列表以及如何以期望的形式返回它们。附:对于奇怪的问题布局感到抱歉 - 如果字符串不是代码格式,字符串在哈希集合之后会一直消失)主要
方法中:
WordGroup
s 来使用 getWordSet()
li>
HashSet
进行迭代或循环,并从中打印出单词
2.a)在 WordGroup
中创建一个名为 getWordCounts() code>其中:
s
HashM ap< String,Integer>
getWordArray()
返回的单词,在$ HashMap
中加入它的次数
HashMap< String,Integer>
$ b 2.b)在主要
getWordCounts()
code> WordGroup
keySet()
来检索一组键值映射的字符串部分)
WordGroup
s
getWordSet()
方法从 WordGroup
s
HashSet
以打印所有单词的完整列表,其中包含每个 HashMap
s
public class main {
public static void main(String [] args){
WordGroup wordgroupOne = new WordGroup(您可以在一小时内发现一个人的更多信息,而不是年的谈话);
WordGroup wordgroupTwo =新的WordGroup(当你在工作时不玩耍时玩耍很难);
String [] quoteOne = wordgroupOne.getWordArray();
String [] quoteTwo = wordgroupTwo.getWordArray();
for(String words:quoteOne){
System.out.println(words);
for(String words:quoteTwo){
System.out.println(words);
$ b
import java.util.HashSet;
import java.util.HashMap;
public class WordGroup {
public String words;
public WordGroup(String getWords){
words = getWords.toLowerCase();
}
public String [] getWordArray(){
return words.split();
}
public HashSet< String> getWordSet(){
HashSet< String> set = new HashSet< String>();
for(String words:quoteOne){
words.add(word);
}
返回单词;
}
public HashMap< String,Integer> getWordCounts(){
HashMap< String,Integer> map = new HashMap< String,Integer>();
for(String words:words){
words.add(word);
}
返回HashMap< String,Integer>;
}
}
有一些基本的错误,你需要先解决,然后才能继续。
让我们先看看这个方法。
public HashSet< String> getWordSet(){
HashSet< String> set = new HashSet< String>();
for(String words:quoteOne){
words.add(word);
}
返回单词;
$ / code>
现在,您还没有设置 quoteOne
到任何东西,至少不是在这个类中。而你想要的是调用 getWordArray()
的返回值。所以最好包含这行
String [] quoteOne = getWordArray();
在这个方法中的某处。
下一步,你正试图重用一个变量名称。你已经在这个类中得到了 words
,所以为了避免混淆,如果你为 String $使用不同的名字会更好。 c $ c>你通过循环迭代的变量。
现在,你正试图将字符串添加到 因此,仔细考虑每个变量的用途,并确保正确使用每个变量。这些类型的错误中的一些会被编译器捕获,但这并不是你使用变量时sl ex的借口。 实际上有更短的转换方法数组放入 现在我们来看下一个方法。 你有正确的基本想法,但你错过了一对夫妇关键步骤。首先,就像在前面的方法中一样,你需要调用 在遍历循环时,您需要查看 另外,请注意,将东西添加到任何地图的方法不是 您可以在此调用中看到关键字和值。 最后,考虑你的 祝你完成作业。我试图指出你正确的方向,而不会为你做太多这样的事。 I am coding in blueJ and what I am trying to do is this: 1.a) Create a 1.b) In the 2.a) Create a method in the 2.b) In the My code so far: WordGroup class: I have got this far and now I am stuck. I cannot figure out how to get the words from teh array into the hashset and hashmap and how to return them in the desired form. p.s. sorry about the wierd question layout- the string kept disappearing after hashset if it was not in the code format) There are some basic mistakes here that you'll need to sort out before you can go any further. Let's look at this method first. Now, you haven't set somewhere in this method. Next, you're trying to reuse a variable name. You've already got Now, you're trying to add strings to So think very carefully about what each variable is for, and make sure that you use each of them correctly. Some of these types of errors will be caught by the compiler, but that's not an excuse for being sloppy with your use of variables. There are actually shorter ways of turning an array into a Now let's look at the next method. You've got the right basic idea, but you're missing a couple of key steps. Firstly, just like in the previous method, you'll need a call to As you iterate through the loop, you'll need to look in the Also, please be aware that the method for adding things to any sort of map isn't You can see the key and the value in this call. Lastly, think about your Good luck with finishing your assignment. I've tried to point you in the right directions without doing too much of this for you. 这篇关于将数组中的单词放入HashMap和HashSet中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! words $ c $当你真的想把它们添加到
set
时,因为 set
是你要返回的东西从这个方法。因此,除了使用 add
更改行外,您还需要将行更改为 return
,以使一切都相配。
HashSet
中,但我认为如果您尝试正确地使用这种方法,这将是一个很好的练习。
public HashMap< String,Integer> getWordCounts(){
HashMap< String,Integer> map = new HashMap< String,Integer>();
for(String words:words){
words.add(word);
}
返回HashMap< String,Integer>;
}
getWordArray()
。但现在,你需要多一步。
HashMap
,使用 get
方法,以查看某个特定单词是否已经在那里记录。如果是这样,你需要看看> Integer
是否已经被记录,添加一个,然后放回地图。如果不是的话,你可以在没有任何算术的情况下将一些东西放到地图中。
add
,它是 put
,它需要两个参数 - 键和值。因此,在你的逻辑中的某个时刻,你可能会有一行像
map.put(word,1);
return
语句。你想返回一个变量,而不是它的类型。你能猜到你将使用哪个变量吗?
getWordSet()
method in WordGroup
which:
WordGroup
object as a parametera HashSet<String>
WordGroup
into the HashSet
HashSet<String>
main
method:
getWordSet()
method using the two WordGroup
sHashSet
returned and print the words from itWordGroup
called getWordCounts()
which:
HashMap<String, Integer>
getWordArray()
and puts each word into the HashMap
with the number of times it occursHashMap<String, Integer>
main
method:
getWordCounts()
on the two WordGroup
skeySet()
to retrieve the set of keys (the String part of the mapping)WordGroup
s getWordSet()
method to make complete set of all the words from both WordGroup
sHashSet
to print a complete list of all words with the sum counts from each of the HashMap
spublic class Main{
public static void main(String[] args){
WordGroup wordgroupOne= new WordGroup ("You can discover more about a person in an hour of play than in a year of conversation");
WordGroup wordgroupTwo= new WordGroup ( "When you play play hard when you work dont play at all");
String[] quoteOne = wordgroupOne.getWordArray();
String[] quoteTwo = wordgroupTwo.getWordArray();
for (String words : quoteOne){
System.out.println(words);
}
for (String words : quoteTwo){
System.out.println(words);
}
}
}
import java.util.HashSet;
import java.util.HashMap;
public class WordGroup {
public String words;
public WordGroup (String getWords){
words = getWords.toLowerCase();
}
public String[] getWordArray(){
return words.split(" ");
}
public HashSet<String> getWordSet(){
HashSet<String> set = new HashSet<String>();
for (String words : quoteOne){
words.add(word);
}
return words;
}
public HashMap<String, Integer> getWordCounts() {
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (String words : words) {
words.add(word);
}
return HashMap<String, Integer>;
}
}
public HashSet<String> getWordSet(){
HashSet<String> set = new HashSet<String>();
for (String words : quoteOne){
words.add(word);
}
return words;
}
quoteOne
to anything yet, at least not in this class. And what you want it to be is the return value from calling getWordArray()
. So it would be good to include the lineString[] quoteOne = getWordArray();
words
in this class, so to avoid confusion, it would be better if you use a different name for the String
variable that you iterate through the loop with.words
, when you actually want to add them to set
, because set
is the thing that you'll be returning from this method. So as well as changing the line with add
, you'll also want to change the line with return
, to make everything match. HashSet
, but I think it would be a good exercise if you try to get it correct, using this way of doing it first.public HashMap<String, Integer> getWordCounts() {
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (String words : words) {
words.add(word);
}
return HashMap<String, Integer>;
}
getWordArray()
. But now, you need one more step. HashMap
, using the get
method, to see if a particular word has already been recorded there. If so, you'll need to see what Integer
has been recorded against it, add one, then put it back in the map. If not, you can just put something into the map without doing any arithmetic.add
, it's put
, and it needs two arguments - the key and the value. So at some point in your logic, you might have a line likemap.put(word, 1);
return
statement. You want to return a variable, not its type. Can you guess which variable you'll use there?