2017-11-11 12 views
2

メソッドに入力された文字列の文字の頻度を検索するコードを記述しようとしています(phraseList()は、単語を取り、各文字をarrayListに入れます) と文字とその頻度を持つ新しいListを返します。私のコードは以下の通りです。ArrayList内の要素の頻度

public List <String> ltrfrq(String phrase){ 
List <String> list0 = new ArrayList<String>(); 
int count = 1; 
List <String> list = phraseList(phrase.toUpperCase()); 
for(int i = 0; i < list.size(); i++){ 
    for(int j = i + 1; j < list.size(); j++){ 
    if(list.get(i).equals(list.get(j))){ 
     count++; 
    } 
    } 
    if(list.get(i).equals(" ")){ 
    list0.add("Space" + "-" + count); 
    } 
    else{ 
    list0.add(list.get(i) + "-" + count); 
    } 
    count = 1; 
} 
return list0; 
    } 
} 

私の問題は、しかし、私は

list.remove(list.get(i)); 
i--; 

のようなものを持っていた、それはすべての文字を返し、私はremove()メソッドを使用してのようにそれらを削除するために多くの方法を試してみましたが、それはまだ動作しないということです誰でも助けてくれますか?ありがとうございました。

+0

HashMap を試したことがありますか? –

+0

@ジャバリダッシュ私はそれをカバーしていません。 – toBiloBa

答えて

3

ハッシュマップはキーと値のペアです。しかし、鍵はユニークです。したがって、重複するキーを持つことはできません。辞書のように、単語の値(定義)を更新することはできますが、その単語は2回は表示されません。

ウォッチ:https://www.youtube.com/watch?v=j442WG8YzM4

読む:https://beginnersbook.com/2013/12/hashmap-in-java-with-example/

出力:

{a=4, b=3, c=2, d=1} 

私はあなたがマップを横断するための練習としてそれを残しておきます。

import java.util.HashMap; 

public class F { 

    public static void main(String[] args) { 

    String string = "aaaabbbccd"; 

    HashMap<Character, Integer> map = frequency(string); 

    System.out.println(map); 
    } 

    public static HashMap<Character, Integer> frequency(String string) { 
    int length = string.length(); 
    char c; 

    HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 

    for (int i = 0; i < length; i++) { 
     c = string.charAt(i); 

     if (map.containsKey(c)) { 
     map.put(c, map.get(c) + 1); 

     } else { 

     map.put(c, 1); 
     } 
    } 

    return map; 
    } 
} 
+0

ありがとう、これはいいですが、HashMapsよりもこれを行う他の方法はありませんか?私はそれにはまだ慣れていないので、私はこれを言っていますが、私はそれをとにかく学ばなければならないことを知っています。 – toBiloBa

+0

することができます...私はアルゴリズムがかなり複雑になると思う...ハッシュマップの仕組みを勉強するよりも複雑かもしれませんが、見苦しいです。 –

+0

ありがとう、ありがとう.... – toBiloBa

2

私はリストデータ構造(および私自身のカスタムデータ)を使用することができますが、これは私がやる方法です。私は重複したエントリを補うために、すべての追加、削除関数を再定義します。

出力:

[{a=4}, {b=3}, {c=2}, {d=1}] 

コード:

import java.util.List; 
import java.util.ArrayList; 

public class F { 

    static class Entry { 
    char character; 
    int count; 

    public Entry(char c, int i) { 
     character = c; 
     count = i; 
    } 

    public String toString() { 
     return "{" + character + "=" + count + "}"; 
    } 
    } 

    public static void main(String[] args) { 

    String string = "aaaabbbccd"; 

    List<Entry> list = frequency(string); 

    System.out.println(list); 
    } 


    public static List<Entry> frequency(String string) { 
    int length = string.length(); 
    char c; 
    Entry entry; 

    List<Entry> list = new ArrayList<Entry>(); 

    for (int i = 0; i < length; i++) { 
     c = string.charAt(i); 

     // add to list 
     add(c, list); 
    } 

    return list; 
    } 

    public static void add(char c, List<Entry> list) { 

    // If the list does not contain the character 
    if (!contains(c, list)) { 
     list.add(new Entry(c, 1)); 

    } else { 

     // Find the entry 
     int index = find(c, list); 

     // If we found the entry's indes 
     if (index >= 0) { 

     // Get the entry 
     Entry temp = list.get(index); 

     temp.count++;   // Increment its count 
     list.remove(index);  // Delete old 1 
     list.add(index, temp); // Insert new 1 
     } 
    } 
    } 

    // Finds the index of an entry thats associated with a character 
    public static int find(char c, List<Entry> list) { 
    int index = -1; 
    int length = list.size(); 


    Entry temp; 


    for (int i = 0; i < length; i++) { 
     temp = list.get(i); 

     if (temp.character == c) { 
     index = i; 
     break; 
     } 
    } 


    return index; 
    } 

    // Remove an Entry from list that is associate with a given character 
    public static List<Entry> remove(char c, List<Entry> list) { 

    for (Entry entry : list) { 
     if (entry.character == c) { 
     list.remove(entry); 
     } 
    } 

    return list; 
    } 

    // Get the entry that correlates to a give character in the list 
    public static Entry get(char c, List<Entry> list) { 
    Entry entryToReturn = null; 

    for (Entry entry : list) { 
     if (entry.character == c) { 
     entryToReturn = entry; 
     break; 
     } 
    } 

    return entryToReturn; 
    } 


    // Checks if the list contains the character 
    public static boolean contains(char c, List<Entry> list) { 
    boolean contains = false; 

    for (Entry entry : list) { 
     if (entry.character == c) { 
     contains = true; 
     break; 
     } 
    } 

    return contains; 
    } 
} 
+0

それはありがとう、私は別の方法にも手がかりを持って知っている、ありがとうございます – toBiloBa

+0

問題はありません。しかし、実際にそれらのハッシュマップを検討してください! –

1

HashMapのは、この問題を解決するための簡単な方法です。リストを使用する必要がある場合は、最初に文字がlist0に存在するかどうかを確認できます。文字がlist0に存在しない場合は、その頻度をカウントします。

更新されたコード:ここで

public static void main(String args[]){ 
    ArrayList <String> list0 = new ArrayList<String>(); 
     int count = 1; 
     //List <String> list = phraseList(phrase.toUpperCase());\ 
     ArrayList<String> list = new ArrayList<String>(); 
     list.add("a"); 
     list.add("b"); 
     list.add("a"); 
     list.add("c"); 
     list.add("b"); 
     list.add("a"); 

     for(int i = 0; i < list.size(); i++){ 
      boolean isDuplicate = false; 
      for (String s: list0){ 
       if (s.contains(list.get(i).trim())) 
        isDuplicate =true; 
      } 

      if (!isDuplicate){ 

        for(int j = i + 1; j < list.size(); j++){ 
        if(list.get(i).equals(list.get(j))){ 
         count++; 
        } 
        } 
        if(list.get(i).equals("/s")){ 
        list0.add("Space" + "-" + count); 
        } 
        else{ 
        list0.add(list.get(i) + "-" + count); 
        } 
        count = 1;  
      } 
     } 
     for (String a: list0) 
      System.out.println(a);   
} 
+0

これが完了しているかどうかわかりませんが、チェックしても機能しませんでした – toBiloBa

+0

何の出力が得られますか? – Maggie

+0

ifステートメントを持たないのと同じです – toBiloBa

1

はJava8 Mapで利用できる新しい方法merge()を使用してそれを実行する方法です。

import java.util.HashMap; 
import java.util.Map; 

public class CountLetterFrequency { 

    public static void main(String[] args) { 
     System.out.println(ltrfrq("abacacdea")); 
    } 

    public static Map<Character, Integer> ltrfrq(String phrase){ 
     Map<Character, Integer> frqMap = new HashMap<>(); 
     for(int i=0; i<phrase.length(); i++){ 
      frqMap.merge(phrase.charAt(i), 1, Integer::sum); 
     } 
     return frqMap; 
    } 
} 

出力:メソッドmerge()

{a=4, b=1, c=2, d=1, e=1} 

、アイテムがマップにない場合には、それだけで、この場合にkey=charAt(i),value=1を追加している、それが追加されます。一方、キーが既にマップ上にある場合、mergeは現在の値と新しい値の両方を渡す関数を呼び出し、この関数の結果でマップを更新します。

Integer::sumはメソッド参照です。mergeメソッドでは2つのパラメータを持つ関数が必要であるため、(currV,newV) -> currV+newVと書き直すことができます。

希望すれば、代わりに新しいStream APIを使用できます。まず、StringIntStreamに変換し、intCharacterにそれぞれマップし、結果をHashMapに収集して返します。 ltrfrqの方法は次のようになります。

public static Map<Character, Integer> ltrfrq(String phrase){ 
    return phrase.chars() 
     .mapToObj(i->(char)i) 
     .collect(HashMap::new, 
      (m,k) -> m.merge(k, 1, Integer::sum), 
      Map::putAll); 
} 
関連する問題