我有一个Integer
类型ArrayList
,我希望遍历我的列表并能够匹配当前索引项的所有重复项。
for(int i = 0; i < list.size(); i++) {
if(list.get(i) == list.get())//stuck here
//I want to scan the list if the current element has the same value to
//other elements while ignoring the value of my current index
任何帮助将不胜感激。谢谢!
答案 0 :(得分:1)
应该是这样的:
int currentIndex = 3;
T value = list.get(currentIndex);
Iterator<T> it = list.iterator();
for (int i = 0; it.hasNext(); i++) {
T checkValue = it.next();
if (i == currentIndex) continue;
if (value != checkValue) continue;
// whatever should take place in case of equality
}
我认为对于“重复”,你实际上是指对象在列表中的相同实例而不是对象是相等的。在这种情况下,应使用!value.equals(checkValue)
(附加检查值不为空以防止异常)
答案 1 :(得分:0)
for(int i = 0; i < list.size(); i++) {
// I have used Equal here but you can use "==" if you need to do // it stronge
list.stream().filter(t->!t.equal(list.get(i))).foreach(t->{
// do what you want to do here
})
}
答案 2 :(得分:0)
我会尝试更合乎逻辑的方法:
Day