2016-04-30 114 views
0

該程序應該只輸出一個字符串中的一個字符,然後指定該字符串中的出現次數。它應該按升序排列,具體取決於特定字符的出現次數。它的工作除了在(char)i部分。它與ASCII代碼或什麼有關?

輸出一個字符串中的一個字符c#

所需的輸出:
B:1
d:1
一個:2個
S:2


代碼的輸出:
U:1
Y:1
þ:2
ÿ:2

public class HuffmanCode { 
    static String string; 
    static Scanner input = new Scanner(System.in); 

    public static void main(String args[]){ 
     System.out.print("Enter a string: "); 
     string = input.nextLine(); 

     int count[] = countOccurence(string); 
     Arrays.sort(count); 

     for (int i = 0; i < count.length; i++) { 
      if (count[i] > 0) 
       System.out.println((char)i + ": " + count[i]); 
     } 
    } 

    public static int[] countOccurence(String str){ 
     int counts[] = new int[256]; 

     for(int i=0;i<str.length();i++){ 
      char charAt = str.charAt(i); 
      counts[(int)charAt]++; 
     } 

     return counts; 
    } 
} 
+1

刪除'Arrays.sort(計數);' – saka1029

+0

@ saka1029我想將它按升序排序。我應該只是硬編碼的排序,所以它會工作? – Meryel

+2

您需要一個包含char和count的對象,創建一個List並對Lis進行排序。順便說一下:它是java,而不是c#,不是嗎? – Turo

回答

0

實施創建一個列表,並對其進行排序而不是排序count

List<int[]> list = new ArrayList<>(); 
    for (int i = 0; i < count.length; i++) { 
     if (count[i] > 0) 
      list.add(new int[] {i , count[i]}); 
    } 
    Collections.sort(list, Comparator.comparing(a -> a[1])); 
    for (int[] a : list) { 
     System.out.println((char)a[0] + ": " + a[1]); 
    } 
+0

謝謝!它的工作原理和代碼很短! – Meryel

+1

@ joel314的答案比這個短。他的回答不需要'countOccurence',它可以正確計數255以上的字符。 – saka1029

+0

謝謝@ saka1029是的,從技術上講,它只是在一行代碼中完成;-) – joel314

0

你可以使用TreeMap定製Comparator

這裏的組合是一個例子

String test = "ABBCCCDDDDEEEEEFFFFFF"; 

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

for (Character c : test.toCharArray()) { 
    if (!map.containsKey(c)) map.put(c, 0); 
    map.put(c, map.get(c) + 1); 
} 

Map<Character, Integer> tMap = new TreeMap<>(new MyComparator(map)); 
tMap.putAll(map); 

for (Map.Entry<Character, Integer> entry : tMap.entrySet()) { 
    System.out.println(entry.getKey() + ": " + entry.getValue()); 
} 

而這裏的MyComparator

class MyComparator implements Comparator<Object> { 

    Map<Character, Integer> map; 

    public MyComparator(Map<Character, Integer> map) { 
     this.map = map; 
    } 

    public int compare(Object o1, Object o2) { 
     if (map.get(o1).equals(map.get(o2))) 
      return 1; 
     else 
      return (map.get(o1)).compareTo(map.get(o2)); 
    } 
} 
+0

排序失蹤 – Turo

+0

無需排序。 'TreeMap'會自動進行排序 – 2016-04-30 09:21:16

+0

是的,但在char之後,而不是計數 – Turo

1

在Java 8中,您可以使用StreamAPI,做這樣的事情:

String input = "ababcabcd" ; 

    input.chars() // split the string to a stream of int representing the chars 
     .boxed() // convert to stream of Integer 
     .collect(Collectors.groupingBy(c->c,Collectors.counting())) // aggregate by counting the letters 
     .entrySet() // collection of entries (key, value), i.e. char, count 
     .stream() // corresponding stream 
     .sorted(Map.Entry.comparingByValue()) // sort by value, i.e. by number of occurence of letters 
     .forEach(e->System.out.println((char)(int)e.getKey() + ": " + e.getValue())); // Output the result 

其結果將是:

d: 1 
c: 2 
a: 3 
b: 3 

我希望它能幫助。

編輯: 假設你輸入

 String input = "ababc\u0327abçd" ; 

我們將不得不在輸入這種情況下ababçabçd,我們需要標準化,以確保我們正確計算是一樣的,有不同的表示字母。爲了實現這一目標,我們使用進行預處理的Normalizationinput,這是在JDK6介紹:

 input = Normalizer.normalize(input, Form.NFC); 
+1

添加字符串規範化,這個答案是黃金。 ('test =「façadefac\ u0327ade」') –

+0

好點,謝謝。剛剛添加了你的建議。 – joel314

相關問題