【发布时间】:2017-03-16 00:36:43
【问题描述】:
有一个这样的 ArrayList。想知道重复了多少个值: 示例:人:4 次,女孩:4 次
public ArrayList<String> new_contains_all= new ArrayList<>();
...
[[laying, beautiful, brushing, group, young, head, bed, close, couple, female, teeth, white, indoor, pair, holding, sunglasses, stuffed, wearing, posing, dress, person, hair, standing, looking, girl, red, playing, made, top, woman, lady, hat, clothing, goggles, people],
[person, hair, young, shirt, looking, phone, girl, white, glasses, food, sunglasses, wearing, woman, hat], [blue, beautiful, person, cake, standing, young, shirt, black, female, board, girl, white, photo, holding, ready, wearing, top, posing, woman, lady, smiling, clothing, dress, suitcase], [street, clock, light, stop, sign, red, text, sitting,
man, photo, traffic], [blue, person, young, looking, phone, sitting, female, girl, photo, computer, using, red, laptop, holding, glasses, food, sunglasses, wearing, top, woman, lady, hat]]
通知 Array 里面有 5 个元素。
已经尝试过了,这应该适用于简单的 ArrayList,但每个元素有超过 1 个字符串,这令人毛骨悚然,我知道解决方案必须在键内实现另一个 bucle,但今天太累了:/ 也许有人让我眨眼:P
List<String> l = new_contains_all;
Set<String> s = new HashSet<String>(l);
for (String key : s) {
int count = Collections.frequency(l, key);
if (count > 1)
System.out.println("Found '" + key + "' " + count + " times."+"key: "+key.toString()+"l: "+l.toString());
}
【问题讨论】:
-
提示:你得到的名字真的很糟糕。 A) 你不使用 _ 作为 Java 名称(SOME_CONSTANT 除外) B) 单字符名称很糟糕;他们唯一的地方是一些 for loop int counter i, j, ...
-
感谢您的回答,只是一个随机名称的测试,@marmor 不是同一个问题,ofc 我已经看到了这个条目