java 试图比较两个迭代器的内容,如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29245381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to compare the contents two Iterators, how?
提问by user286152
EDIT: With your help I managed to fix my problem. I have edited my code to now show how I had to have it set up to get it working.
编辑:在您的帮助下,我设法解决了我的问题。我已经编辑了我的代码,现在显示我必须如何设置它才能使其正常工作。
Currently I am having trouble coding a part which compares the content of two iterators. As part of the requirements for my assignment, I need to use a linkedlist to store the individual characters of the entered String. I have gotten to the point where I have two iterators which would contain the input one way and the reverse way.
目前我在编写比较两个迭代器内容的部分时遇到问题。作为我的作业要求的一部分,我需要使用链表来存储输入字符串的各个字符。我已经到了我有两个迭代器的地步,它们将包含一种方式和相反方式的输入。
String palindrom = input.getText();
String [] chara = palindrom.split (""); //this is successfully splitting them, tested.
int length = palindrom.length( ); // length == 8
System.out.println (length); //can use this for how many checks to do?
LinkedList ll = new LinkedList(Arrays.asList(chara));
Iterator iterator = ll.iterator();
Iterator desIterator = ll.descendingIterator();
/*while(iterator.hasNext() ){
System.out.println(iterator.next() );
}
while(desIterator.hasNext() ){
System.out.println(desIterator.next() );
}*/
boolean same = true;
while(iterator.hasNext()){
if(!iterator.next().equals(desIterator.next())){
same = false;
break;
}
}
And using the System.out I can see that they are being stored correctly, but I don't know how to check if the iterators store the same contents. What would be one of the simplest methods to compare the two iterators or convert them into something I can compare? To clarify I want to verify they contain the same elements in the same order.
使用 System.out 我可以看到它们被正确存储,但我不知道如何检查迭代器是否存储相同的内容。比较两个迭代器或将它们转换为我可以比较的东西的最简单方法之一是什么?为了澄清我想验证它们以相同的顺序包含相同的元素。
回答by satnam
boolean same = true;
while(iterator.hasNext()){
if(!desIterator.hasNext() || !iterator.next().equals(desIterator.next())){
same = false;
break;
}
}
System.out.println(same);
回答by Adrian Leonhard
You need to iterate over both iterators simultaneously, i.e. with one loop. Here is a general comparison function (0 when equal, < 0 when A < B, > 0 when A > B):
您需要同时迭代两个迭代器,即一个循环。这是一个通用的比较函数(相等时为 0,A < B 时 < 0,A > B 时 > 0):
static <T extends Comparable<S>, S> int compare(Iterator<T> a, Iterator<S> b) {
while (a.hasNext() && b.hasNext()) {
int comparison = a.next().compareTo(b.next());
if (comparison != 0) {
return comparison;
}
}
if (a.hasNext())
return 1;
if (b.hasNext())
return -1;
return 0;
}
To just check if they are equal, this can be simplified:
为了检查它们是否相等,这可以简化:
static <T, S> boolean equals(Iterator<T> a, Iterator<S> b) {
while (a.hasNext() && b.hasNext()) {
if (!a.next().equals(b.next())) {
return false;
}
}
if (a.hasNext() || b.hasNext()) {
// one of the iterators has more elements than the other
return false;
}
return true;
}
Guava implements this as Iterators.elementsEqual.
Guava 将其实现为Iterators.elementsEqual。
回答by Arsen Avalyan
In both answers throw NullPointerException, if iterator.next() == null. This method is more optimal.
如果 iterator.next() == null,则在两个答案中都抛出 NullPointerException。这种方法更优。
public static boolean equals(Iterator i1, Iterator i2) {
if (i1 == i2) {
return true;
}
while (i1.hasNext()) {
if (!i2.hasNext()) {
return false;
}
if (!Objects.equals(i1.next(), i2.next())) {
return false;
}
}
if (i2.hasNext()) {
return false;
}
return true;
}