将每个项目与ArrayList中的每个其他项目进行比较 [英] Compare every item to every other item in ArrayList

查看:88
本文介绍了将每个项目与ArrayList中的每个其他项目进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了我认为应该是一个非常简单的问题。

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中的每个其他项目进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆