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)

4

5 回答 5

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);
}
于 2013-04-17T15:37:12.300 回答
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()+")");
       } 
于 2013-04-17T15:43:34.073 回答
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.

因此,如果您想要所有唯一值,您可以遍历键集并从 Map 中获取相应的计数。

于 2013-04-17T15:29:19.757 回答
0

我可能的简单方法是

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

然后玩 directionS

于 2013-04-17T15:30:31.540 回答
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)***
于 2013-04-17T15:45:03.030 回答