[英]How to get how many times an element occurs in ArrayList
for(int i=0; i<arr.size(); ++i){
oc = Collections.frequency(arr, arr.get(i));
System.out.print(oc + " "+ arr.get(i) +" ");
arr.remove(i);
}
The main idea is to output how many times every element in sequence occurs. 主要思想是输出序列中每个元素出现多少次。 For example
例如
1 1 2 3 3 3 10 10
here the output is 这里的输出是
2 1 1 2 3 3 2 10
It is like two ones, one element of two, 3 elements of 3, and 2 elements of 10. 就像两个,一个元素为两个,三个元素为3,两个元素为10。
This is buggy, for example it is not working for this case 这是越野车,例如在这种情况下不起作用
1 1 1 2 2 1 1 1
What is wrong? 怎么了? Any other algorithms?
还有其他算法吗?
The problem is that inside the for
loop you remove an element ( arr.remove(i)
) so the remaining elements get shifted and when i
gets incremented, you skip one element. 问题是,在
for
循环中,您删除了一个元素( arr.remove(i)
),以便其余元素被移位,而当i
递增时,您将跳过一个元素。 Removing an element also changes its frequency, so don't do that. 删除元素也会改变其频率,因此请不要这样做。
Do something like this: 做这样的事情:
List<String> arr = Arrays.asList("a", "a", "b", "a", "a");
for (String s : arr)
System.out.println("element: " + s
+ ", count: " + Collections.frequency(arr, s));
If an element is in the list multiple times, this will print it multiple times. 如果元素多次在列表中,则将多次打印该元素。 Use a
HashSet
to remember if an element was already printed, and do not print it again: 使用
HashSet
记住元素是否已经被打印,并且不要再次打印它:
List<String> arr = Arrays.asList("a", "a", "b", "a", "a");
Set<String> printed = new HashSet<>();
for (String s : arr) {
if (printed.add(s)) // Set.add() also tells if the element was in the Set!
System.out.println("element: " + s
+ ", count: " + Collections.frequency(arr, s));
}
Output: 输出:
element: a, count: 4
element: b, count: 1
Alternatively you can add all elements of the original list to a Set
(which will ensure every element is contained only once), and iterate over this set, but count in the original array: 或者,您可以将原始列表的所有元素添加到
Set
(这将确保每个元素仅包含一次),并遍历此set,但要计入原始数组中:
List<String> arr = Arrays.asList("a", "a", "b", "a", "a");
for (String s : new HashSet<>(arr))
System.out.println("element: " + s
+ ", count: " + Collections.frequency(arr, s));
Output: same. 输出:相同。 But note that this might result in different order of the output as
Set
s in Java are not ordered. 但是请注意,这可能会导致输出顺序不同,因为Java中的
Set
没有排序。
I would work with a HashMap, whereas the meaning is element -> count! 我将使用HashMap,而含义是元素->计数! pseudo:
伪:
HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>();
for(int i=0; i<arr.size(); ++i){
Integer x = counts.get(arr.get(i));
if (x==null) counts.put(arr.get(i), 1);
else counts.put(arr.get(i), x+1);
}
after this, your hashmap holds all elements and their count 之后,您的哈希图将保存所有元素及其数量
try this 尝试这个
List a = Arrays.asList(1, 2, 1, 3, 1);
Collections.sort(a);
Object o = a.get(0);
int n = 1;
for (int i = 1; i < a.size(); i++) {
Object t = a.get(i);
if (o.equals(t)) {
n++;
} else {
System.out.println(o + " - " + n);
n = 1;
o = t;
}
}
System.out.println(o + " - " + n);
output 输出
1 - 3
2 - 1
3 - 1
快速,聪明的方法是:1)使用Collections.sort对arrayList进行排序2)使用indexOf()获取第一个索引,并使用lastIndexOf()方法获取最后一个索引3)2个索引的差将为您提供出现次数给定对象在ArrayList中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.