问题描述
我遇到了一个我认为应该很简单的问题.
I'm having trouble with what I thought should be a pretty simple problem.
我需要将 arrayList 中的每个项目与列表中的每个其他项目进行比较,而不需要将项目与其自身进行比较.它不像调用 equals() 比较那么简单,它涉及一些我在下面的代码中省略的自定义逻辑.此外,不应以任何方式更改 ArrayList.
I need to compare every item in an arrayList with every other item in the the list without comparing items to themselves. It's not as simple as calling an equals() comparison, it involves some custom logic that I've omitted from my code below. Also the ArrayList should not be changed in any way.
我似乎遇到的问题是,一旦进入第二个循环,我不知道是否还有另一个对象要进行比较(因为它是一个可变大小的列表).
The problem I seem to be having is that once I get into the second loop, I don't know if I have another object to compare to (since its a variable sized list).
for(int i =0; i< list.size(); i++){
//get first object to compare to
String a = list.get(i).getA();
Iterator itr = list.listIterator(i + 1 ); // I don't know if i + 1 is valid
while(itr.hasNext()){
// compare A to all remaining items on list
}
}
我想我可能以错误的方式解决这个问题,我愿意接受有关如何更好地做到这一点的建议或提示.
I think I probably going about this the wrong way, I'm open to suggestions or tips on how to do this better.
推荐答案
for (int i = 0; i < list.size(); i++) {
for (int j = i+1; j < list.size(); j++) {
// compare list.get(i) and list.get(j)
}
}
这篇关于将每个项目与 ArrayList 中的每个其他项目进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!