-1
一个字符串数组存在我有一个字符串数组是这样的:多少具体的价值在安卓
String[] sample = { "0", "1", "0", "5", "1", "0" };
现在我需要知道有多少特定值这样的数组中存在0。 所以我怎么能得到这个?
一个字符串数组存在我有一个字符串数组是这样的:多少具体的价值在安卓
String[] sample = { "0", "1", "0", "5", "1", "0" };
现在我需要知道有多少特定值这样的数组中存在0。 所以我怎么能得到这个?
希望这可以帮助你..
int count = 0;
String[] array = new String[]{"a", "a", "d", "c", "d", "c", "v"};
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(array));
for (int i = 0; i < array.length; i++) {
count = 0;
for (int j = 0; j < arrayList.size(); j++) {
if (arrayList.get(j).equals(array[i])) {
count++;
}
}
System.out.println("Occurance of " + array[i] + " in Array is : " + count);
}
尝试HashMap
跟踪数组中每个单词的出现次数。
public static void main(String[] args) {
String[] sample = { "0", "1", "0", "5", "1", "0" };
Map<String, Integer> map = new HashMap<>();
for (String s : sample) {
Integer n = map.get(s);
n = (n == null) ? 1 : ++n;
map.put(s,n);
}
System.out.println(map);
}
输出:(希望这是你想要的)
{1=2, 0=3, 5=1}
的迭代地图使用:
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " occurs = " + pair.getValue()+" times");
}
输出:
1 occurs = 2 times
0 occurs = 3 times
5 occurs = 1 times
您是否正在计算数组中唯一值的数量? – kkaosninja