2016-11-11 88 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對象轉換爲字符串,然後你可以輕鬆地打印出來。