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)
陣列的所有元素加入到[設置](http://docs.oracle.com/javase/6/docs/api /java/util/HashSet.html)。 – Henrik
數組是否總是被排序? – NINCOMPOOP