2016-11-11 89 views
1

对于我的任务,我一直要求出示以下:如何使用增强for循环遍历另一个方法中的HashSet?

  • 创建HashMap<String,Integer>
  • 遍历由getWordArray()返回的所有单词,并把每个单词到HashMap用的次数就发生
  • 返回HashMap<String, Integer>

我已经创建了内部调用getWordArray()另一种方法HashSet的,但我struggli ng找出如何使它for循环迭代通过getWordSet()方法而不会出现错误“non-static方法不能从静态上下文中引用”。当我返回创建的HashSet字符串时,出现错误“不兼容的类型:

HashMap<String, Integer>无法转换为字符串”。

下面是代码:

WordGroup类

import java.util.HashSet; 
import java.util.Set; 
import java.util.HashMap; 

public class WordGroup { 

String word; 

//Creates constructor which stores a string value in variable "word" and converts this into lower case using the lower case method. 
public WordGroup(String aString) { 
    this.word = aString.toLowerCase(); 
} 
public String[] getWordArray() { 
    String[] wordArray = word.split("-"); 
    return wordArray; 
} 


public Set<String> getWordSet(WordGroup secondWordGroup) { 

HashSet<String> newHashSet = new HashSet<>(); 

for (String word : secondWordGroup.getWordArray()) 
    newHashSet.add(word); 

for (String word : this.getWordArray()) 
    newHashSet.add(word); 
System.out.println(newHashSet); 
return newHashSet; 

} 
public String getWordCounts() 
{ 
    HashMap<String, Integer> myHashMap=new HashMap<String, Integer>(); 
    int loopcounter = 0; 
    for (WordGroup it : WordGroup.getWordArray()) 

    loopcounter = loopcounter +1; 
    myHashMap.add(it); 
    return myHashMap; 
} 
} 

主类

public class Main{ 
    public static void main (String[] args) { 
     WordGroup firstWordGroup = new WordGroup("You-can-discover-more-about-a-person-in-an-hour-of-plau-tban-in-a-year-of-conversation"); 
     WordGroup secondWordGroup = new WordGroup ("When-you-play-play-hard-when-you-work-dont-play-at-all"); 

    } 

我有两个主要的疑问:

  • 如何正确迭代getWordArray方法?
  • 如何返回数据类型的HashMap而不是String?

我看不到我的WordGroup类正在使用的任何静态方法所以很明显我的静态一些误解的地方,我知道了第二个错误,我只是没有就如何解决它太肯定。

在此先感谢。

编辑:

public Set<String> getWordCounts(WordGroup secondWordGroup) 
{ 
    HashMap<String,Integer> myMap; 

    if (myMap.keySet().contains(getWordArray[i])) 
    {  
     myMap.get(getWordArray[i]) +1; 
     } 
    else 
    { 
     myMap.put(getWordArray[i],1); 

    } 
    return myMap; 
} 

我不知道如果是做正确的,但有多个错误:我一直在使用这个if语句试过吗?

+0

对于与静态方法有关的错误,您可以使该方法非静态? 对于与不兼容类型相关的错误,我相信你应该创建一个HashMap 而不是HashSet 。 – ritratt

+0

这就是我感到困惑的地方,我根本没有在WordGroup类中看到static这个词,所以我认为一切都是非静态的? – Alan

+0

我想你想[这](http://stackoverflow.com/questions/19896467/putting-words-from-an-array-into-a-hashmap-and-a-hashset) –

回答

0

首先: HashSet不能有重复值。假设HashSet已经在其中包含单词'hello'。然后添加('你好')什么都不做。 HashSet在那里仍然只有一个单词'hello'。 如果您需要在其中有多个'hello',请改用ArrayList。

第二你在哪里找到HashMap.add()方法?它不在那里。

第三:你削减了一些你的代码?

for (WordGroup it : WordGroup.getWordArray()) 

loopcounter = loopcounter +1; 

那for循环只做loopcounter = loopcounter +1;

没有别的。

这边走: HasMap有一个键字和值occurencies即

HasMap<String,Integer> myMap 

那么当你通过你的wordArray(你不需要其他任何东西)

如果MYMAP数有一个关键wordArray [I]增加它在地图关联值:

myMap.put(wordArray[i],myMap.get(wordArray[i]) +1) 

要不然就把新元素

myMap.put(wordArray[i],1) 

我希望你会写它周围的其他的事情你自己的。

编辑:

public class WordGroup { 

    String word; 

    // Creates constructor which stores a string value in variable "word" and converts this into lower case using 
    // the lower case method. 
    public WordGroup(String aString) { 

     this.word = aString.toLowerCase(); 
    } 

    public String[] getWordArray() { 

     String[] wordArray = word.split("-"); 
     return wordArray; 
    } 

    public HashMap<String, Integer> getWordCountsMap() { 

     HashMap<String, Integer> myHashMap = new HashMap<String, Integer>(); 

     for (String nextWord: this.getWordArray()) { 
      if (myHashMap.keySet().contains(nextWord)) { 
       myHashMap.put(nextWord, myHashMap.get(nextWord) + 1); 
      } else { 
       myHashMap.put(nextWord, 1); 
      } 

     } 

     return myHashMap; 
    } 
} 

末最好不要创建HashMap的每一次,但在构造函数中做一次。

public class WordGroup { 

    String word; 
    HashMap<String, Integer> myHashMap = new HashMap<String, Integer>(); 

    // Creates constructor which stores a string value in variable "word" and converts this into lower case using 
    // the lower case method. 
    public WordGroup(String aString) { 

     this.word = aString.toLowerCase(); 
     this.createWordCountsMap(); 
    } 

    public String[] getWordArray() { 

     String[] wordArray = word.split("-"); 
     return wordArray; 
    } 

    public HashMap<String, Integer> getWordCountsMap() 
    { 
     return this.myHashMap; 
    } 

    private void createWordCountsMap() { 

     for (String nextWord : this.getWordArray()) { 
      if (this.myHashMap.keySet().contains(nextWord)) { 
       this.myHashMap.put(nextWord, this.myHashMap.get(nextWord) + 1); 
      } else { 
       this.myHashMap.put(nextWord, 1); 
      } 
     } 
    } 
} 
+0

这会被放入if/else语句中吗? – Alan

+0

是的。你不需要混淆额外的ArrayList。无论如何,Set不适用于重复值。 – Vadim

+0

if(myMap.keySet()。contains(wordArray [i])) – Vadim

1

首先,getWordCounts()中的for循环没有任何东西。你必须围绕这样

for (/* single element of structure */ : /* structure on which you iterate */) { 
     // what you want to do with this single element 
    } 

您收到第一个错误是关于这部分代码用两个大括号代码:

WordGroup.getWordArray() 

你正试图从类WordGroup调用静态方法来代替对给定的类的实例调用您的非静态方法。你想实现的只是getWordArray()。

另一个错误是for循环中变量“it”的类型。 getWordArray()返回String [],所以这个结构中的单个元素将是String,现在是WordGroup。

你也应该熟悉HashMap的方法和注意,这个结构需要键和值。在你的例子中,字符串是键值和整数值。

回答你的第二个问题,关于打印的HashMap,你可以用它的toString方法HashMap对象转换为字符串,然后你可以轻松地打印出来。