2013-04-17 114 views
0
public class nrlSports { 
    public static void main(String[] args){ 
     String[] direction = {"north", "north", "east", "south", "south", "south", "north", "north"}; 

     for(int i=0; i<direction.length; i++) { 
      int count = 0; 
      for(int j = 0; j < direction.length; j++) 
       if(direction[i].equals(direction[j])) { 
        count++; 
       } 
      System.out.println(direction[i] + " (" + count + ")"); 
     } 
    } 
} 

的輸出是: 北(4) 北(4) 東(1) 南(3) 南(3) 南(3) 北(4) 北(4)如何刪除重複值然後顯示唯一值?

如何刪除這些重複的值,所以輸出應該是這樣的: 北(4) 東(1) 南(3)

+0

陣列的所有元素加入到[設置](http://docs.oracle.com/javase/6/docs/api /java/util/HashSet.html)。 – Henrik

+0

數組是否總是被排序? – NINCOMPOOP

回答

0

如何有關此方法的

String[] direction = { "north", "north", "east", "south", "south", 
     "south", "north", "north" }; 

Map<String, Integer> map = new LinkedHashMap<>();//to preserve order or elements 
for (String key : direction) { 
    if (map.containsKey(key)) 
     map.put(key, map.get(key) + 1); 
    else 
     map.put(key, 1); 
} 
for (Map.Entry<String, Integer> entry : map.entrySet()) 
     System.out.println(entry.getKey() + "(" + entry.getValue() + ")"+ 
       String.format("%"+entry.getValue()+"s", "").replace(' ', '*')); 

輸出

north(4)**** 
east(1)* 
south(3)*** 
+0

是的,這是完美的。如何添加星號*,例如: north(4):**** east(1):* south(3):*** – Shuvo0o

+0

非常感謝。有一件事我不明白是'\ u0000'。那是做什麼的? – Shuvo0o

+0

@ Shuvo0o我改變了代碼,不再使用'\ u0000'。默認情況下,空字符數組是用第一個Unicode字符'\ u0000'填充的,所以整個字符串也只包含它們。現在我正在使用String.format(「%」+ entry.getValue()+「s」,「」)'。如果你像'String.format(「%5s」,「」)那樣使用它,它會產生包含5個空格的String''「'。後來我用'*'替換每個空間。 – Pshemo

0

使用一組。將它們全部添加到一個集合中,您將獲得獨特的值。編輯: 我錯過了計數部分。添加現在

String[] direction = {"north", "north", "east", "south", 
          "south", "south", "north", "north"}; 

    Map<String,int> m = new HashMap<String,int>(); 

for(String str:direction){ 
    if(m.contains(str)) 
     m.put(key,m.get(str)+1); 
    else 
     m.put(key,1); 
} 


//key is all values you want to add and count in no. of times values is repeated. 

所以,如果你希望所有的獨特的價值觀,你可以遍歷鍵集,並從地圖對應的計數。

+0

伯爵呢? – NINCOMPOOP

+0

@Noob:編輯我的解決方案以添加計數部分。 – Lokesh

0

我可以簡單的辦法就是

Set<String> directionS= new HashSet<String>(Arrays.asList(direction)); 

,然後用directionS

1

玩這可以幫助:

public static void main(String[] args){ 
    String[] direction = {"north", "north", "east", "south", 
          "south", "south", "north", "north"}; 
    Map<String, Integer> countMap = new HashMap<String, Integer>(); 

    for(int i=0; i<direction.length; i++) { 
     String key = direction[i]; 
     if(!countMap.containsKey(key)){ 
      countMap.put(key, 1); 
     } 
     else 
     { 
      countMap.put(key, countMap.get(key)+1); 
     } 
    } 
    System.out.println(countMap); 
} 
1

認爲通過運行該解決方案不是最佳的一個,但將工作(已確認它:))

 String[] direction = {"north", "north", "east", "south", "south", "south", "north", "north"}; 
    HashMap<String,Integer> myHash = new HashMap<String, Integer>(); 


    for(int i=0; i<direction.length; i++) { 
     int count = 0; 
     for(int j = 0; j < direction.length ; j++) 
      if(direction[i].equals(direction[j])) { 
       count++; 
       myHash.put(direction[i], new Integer(count));      
      } 
    } 

     Iterator T = (Iterator) myHash.entrySet().iterator(); 
     while(T.hasNext()) 
     { 
      Map.Entry newEntery = (Map.Entry) T.next();     
      System.out.print(newEntery.getKey() +"("+ newEntery.getValue()+")"); 
     } 
+1

@ Shuvo0o做檢查它的傢伙 –