[英]Iterate over a list using an iterator and compare consecutive elements
I want to compare each element in a single list and the code directly below provides me my solution: 我想比较单个列表中的每个元素,下面的代码直接为我提供了解决方案:
for (int i = 0; i < list.size(); i++) {
for (int j = i + 1; j < list.size(); j++) {
// compare
}
}
It compares elements index wise, for instance comparing four elements: 它按索引比较元素,例如比较四个元素:
0 to 1
0 to 2
0 to 3
1 to 2
1 to 3
2 to 3
I want to achieve the same as above using an iterator. 我想使用迭代器实现与上述相同的效果。 My implementation using an iterator:
我的实现使用迭代器:
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;
}
}
}
Tuple
is a class with some string properties. Tuple
是具有某些字符串属性的类。
This code only compares the first element with the others and throws NoSuchElementException
, but I want to move from inner for-loop to the outer for-loop to keep traversing. 这段代码仅将第一个元素与其他元素进行比较,并抛出
NoSuchElementException
,但是我想从内部for循环移动到外部for循环以保持遍历。 How can I achieve this? 我该如何实现?
Hi you get the Exception because of your it.next() within the inner for loop. 嗨,您因为内部for循环中的it.next()而获得Exception。 You need a second Iterator.
您需要第二个Iterator。
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;
}
}
}
To compliment shsvd98 answer , you can use a ListIterator
instead of an Iterator
. 为了配合shsvd98的回答 ,您可以使用
ListIterator
代替的Iterator
。 ListIterator
extends Iterator
and provides more functionality. ListIterator
扩展了Iterator
并提供了更多功能。
For instance the following code does exactly what your first code block does. 例如,以下代码与您的第一个代码块完全一样。 It also doesn't compare an element with itself and there's no need to create another
List
after each outer for-loop iteration. 它还不会将元素与其自身进行比较,并且不需要在每个外部for循环迭代之后创建另一个
List
。
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;
}
}
}
I ran the above code, switching out Tuple
with String
. 我运行了上面的代码,用
String
切换了Tuple
。
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
If your curious about the benefits of using ListIterator
over Iterator
, then the ListIterator
documentation provides more insight. 如果您对使用
ListIterator
不是Iterator
感到好奇,那么ListIterator
文档将提供更多的见解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.