java - compareTo() 方法如何在我的代码中使用 listIterator 工作?
问题描述
由于 listIterator 使用了compareTo()方法,我无法从下面的addInOrder()方法中理解while 循环。我将在下面发布代码以及我没有理解的部分。
public class Demo {
public static void main(String[] args) {
LinkedList<String> placesToVisit = new LinkedList<String>();
placesToVisit.add("Sydney");
placesToVisit.add("Melbourne");
placesToVisit.add("Brisbane");
placesToVisit.add("Perth");
placesToVisit.add("Canberra");
placesToVisit.add("Adelaide");
placesToVisit.add("Darwin");
printList(placesToVisit);
private static void printList(LinkedList<String> linkedList) {
Iterator<String> i= linkedList.iterator();
while(i.hasNext()) {
System.out.println("Now visiting " + i.next());
}
System.out.println("=========================");
}
private static boolean addInOrder(LinkedList<String> linkedList, String newCity) {
ListIterator<String> stringListIterator = linkedList.listIterator();
while(stringListIterator.hasNext()) {
int comparison = stringListIterator.next().compareTo(newCity);
if(comparison == 0) { // equal, do not add
System.out.println(newCity + " is already included as a destination");
return false;
} else if(comparison > 0) { // new City should appear before this one
stringListIterator.previous();
stringListIterator.add(newCity);
return true;
} else if(comparison < 0) {
// move on next city
}
}
stringListIterator.add(newCity); //adds a city in case the linkedList passed as a parameter is empty.
return true;
}
所以,谈谈addInOrder方法。我明白了,通过声明ListIterator stringListIterator = linkedList.listIterator(); ,迭代器的光标将位于链表的第一个元素之前,按照示例,它是Sydney。
假设添加到链表的城市的顺序是main方法中使用的顺序:悉尼、墨尔本、布里斯班、珀斯、堪培拉、阿德莱德和达尔文。
现在,关于while 循环:int comparison = stringListIterator.next().compareTo(newCity); 将光标移到第一个元素之后,即Sydney,它将把Sydney与 newCity 参数进行比较。假设我要传递的 newCity 参数是Adelaide。它将Sydney与Adelaide进行比较,比较后将得到 18 因为Sydney “大于” Adelaide,所以它会进入else if(comparison>0)条件;
通过声明:stringListIterator.previous(); 光标将回到初始位置,我的意思是在第一个元素之前Sydney。第二行,stringListIterator.add(newCity); 就我而言,将添加Adelaide作为linkedList 的第一个元素,Sydney紧随其后。
我的问题是在那之后:因为它是一个while循环,它会重新执行代码,但是我的问题来了:再次到达int comparison = stringListIterator.next().compareTo(newCity); ,光标将位于何处?它将从悉尼和墨尔本之间转移到墨尔本和布里斯班之间?如果是这样,那么将墨尔本与 newCity 参数(如我上面所说的阿德莱德引入)进行比较,将得到 12,因为墨尔本“大于”阿德莱德,这将再次检查条件else if(comparison>0),它将进入它所以,stringListIterator.previous(); 现在将光标在Sydney和Melbourne之间移动,再次添加Adelaide(如第一次比较)?我知道我的逻辑中缺少一些东西,因为代码实际上按字母顺序排列了所有城市(这就是代码的目的),但我在哪里犯了错误???请帮助我,在此先感谢!
解决方案
你是对的; 直到你写下“再次 int comparison = stringListIterator.next().compareTo(newCity);
到达”(强调)
如果你研究一下else if
有一个return true
. 这意味着您不会再次到达while。
...
} else if(comparison > 0) { // new City should appear before this one
stringListIterator.previous();
stringListIterator.add(newCity);
return true;
} else { ... }
推荐阅读
- opengl - WebGL / OpenGL中sampler2D制服的用例是什么?
- c++ - 函数“反射”C++
- jpa - 在 JPQL 中创建新对象只返回一个对象
- .net-core - Azure B2C 和 Core 3.1 API 的正确设置
- java - 如何按键获取嵌套的 JSON 内容?
- python - Python Folium 错误:“并非所有参数都在字符串格式化期间转换”
- java - 在java中,我试图为一个类创建一个对象,但它显示错误“公共类型 Add1 必须在其自己的文件中定义”
- php - 如何修复错误 laravel 7 数组以进行转换
- javascript - TopoJSON:未捕获(承诺中)类型错误:无法读取未定义的属性“对象”
- r - 如何处理R中的列表向量?