Java 使用迭代器在列表上迭代并比较连续的元素

Java 使用迭代器在列表上迭代并比较连续的元素,java,list,loops,iterator,Java,List,Loops,Iterator,我想比较单个列表中的每个元素,下面的代码为我提供了解决方案: for (int i = 0; i < list.size(); i++) { for (int j = i + 1; j < list.size(); j++) { // compare } } 我希望使用迭代器实现与上面相同的功能。我使用迭代器的实现: int i = 0; for (Iterator<Tuple> it = list.iterator(); it.has

我想比较单个列表中的每个元素,下面的代码为我提供了解决方案:

for (int i = 0; i < list.size(); i++) {
    for (int j = i + 1; j < list.size(); j++) {
        // compare 
    }
}
我希望使用迭代器实现与上面相同的功能。我使用迭代器的实现:

int i = 0;
for (Iterator<Tuple> it = list.iterator(); it.hasNext();) {
    Tuple tB = null;
    Tuple tA = it.next();

    for(int j = 0; j == i; j++) {
        try {
            tB = it.next();

            if(tA.Username.equals(tB.Username) && .....) {
                System.out.println("Match");
            } else {
                System.out.println("Not Matched");
            }
            i++;
        } catch(NoSuchElementException exc) {
            throw exc;
        }   
    }
}
inti=0;
for(Iterator it=list.Iterator();it.hasNext();){
元组tB=null;
Tuple tA=it.next();
对于(int j=0;j==i;j++){
试一试{
tB=it.next();
if(tA.Username.equals(tB.Username)&&&…){
System.out.println(“匹配”);
}否则{
系统输出打印项次(“不匹配”);
}
i++;
}捕获(无接触异常exc){
抛出exc;
}   
}
}
Tuple
是一个具有一些字符串属性的类


这段代码只将第一个元素与其他元素进行比较,并抛出
NoSuchElementException
,但我想从内部for循环移动到外部for循环,以保持遍历。如何实现这一点?

您好,由于内部for循环中的it.next()出现异常。您需要第二个迭代器

 for (Iterator<Tuple> it = list.iterator(); it.hasNext();) 
{
       Tuple tA = it.next();
       List<Tuple> tmpList = new ArrayList<Tuple>(list);
       tmpList.remove(tA);
  for( Iterator<Tuple> it2 = tmpList.iterator(); it2.hasNext();)     {
        try{
            Tuple tB=it2.next();

            if(tA.Username.equals(tB.Username) && .....)
            {
                System.out.println("Match");
            }
            else
            {
                System.out.println("Not Matched");
            }
            i++;
        }
        catch(NoSuchElementException exc)
        {
            throw exc;
        }   
    }
}
for(Iterator it=list.Iterator();it.hasNext();)
{
Tuple tA=it.next();
List tmpList=新阵列列表(List);
tmpList.remove(tA);
for(迭代器it2=tmpList.Iterator();it2.hasNext();){
试一试{
Tuple tB=it2.next();
if(tA.Username.equals(tB.Username)&&&…)
{
System.out.println(“匹配”);
}
其他的
{
系统输出打印项次(“不匹配”);
}
i++;
}
捕获(无接触异常exc)
{
抛出exc;
}   
}
}
作为补充,您可以使用
列表迭代器
而不是
迭代器
ListIterator
扩展了
Iterator
并提供了更多功能

例如,下面的代码与第一个代码块完全相同。它也不会将一个元素与自身进行比较,并且在每次外部for循环迭代之后,不需要创建另一个
列表

Tuple tA = null;
Tuple tB = null;

for (ListIterator<Tuple> one = list.listIterator(); one.hasNext();) {
    // set 'tA' here, not in the nested for loop
    tA = one.next();

    // initialize 'two' with the next index of 'one'
    for(ListIterator<Tuple> two = list.listIterator(one.nextIndex()); two.hasNext();) {
        // set 'tB' here
        tB = two.next();

        // debug line used in output below this code block
        // System.out.print("tA:" + tA + " to tB:" + tB + " ");

        try {
            if(tA.username.equals(tB.username)) {
                System.out.println("Match");
            } else {
                System.out.println("Not Matched");
            }
        } catch(NoSuchElementException exc) {
            throw exc;
        }  

    }
}

如果您对使用
列表迭代器
而不是
迭代器
的好处感到好奇,那么提供了更多的见解。

您需要一个内部迭代器和一个外部迭代器。直接访问有什么错?您的代码已经可以运行了。@Bohemian我正在为apache pig编写java udf,出于测试目的,我在纯java上尝试了直接访问,但我使用了apache pig的元组类和列表。我无法使用I.get(arg)访问属性方法..我可以在普通java中实现。Tuple类实现迭代器接口,因此我尝试用java测试相同的方法。它返回两个新的迭代器代码,比较每个对象与另一个对象。它还与自身进行比较…如何防止相同的索引对象不相互比较?我正在尝试相同的方法,您可以使用新的列出并从中删除元素。这里将item1与item2进行比较,然后再将item2与item2进行比较…那么我们如何停止这种比较呢?我需要迭代哪个实现了Iterable,所以不能使用ListIterator。有什么方法吗?当我试图将数据包转换为列表时,它引发了类强制转换异常。。
Tuple tA = null;
Tuple tB = null;

for (ListIterator<Tuple> one = list.listIterator(); one.hasNext();) {
    // set 'tA' here, not in the nested for loop
    tA = one.next();

    // initialize 'two' with the next index of 'one'
    for(ListIterator<Tuple> two = list.listIterator(one.nextIndex()); two.hasNext();) {
        // set 'tB' here
        tB = two.next();

        // debug line used in output below this code block
        // System.out.print("tA:" + tA + " to tB:" + tB + " ");

        try {
            if(tA.username.equals(tB.username)) {
                System.out.println("Match");
            } else {
                System.out.println("Not Matched");
            }
        } catch(NoSuchElementException exc) {
            throw exc;
        }  

    }
}
Populated ArrayList with ["a", "b", "c", "b"]

Output:
tA:a to tB:b Not Matched
tA:a to tB:c Not Matched
tA:a to tB:b Not Matched
tA:b to tB:c Not Matched
tA:b to tB:b Match
tA:c to tB:b Not Matched