首页 > 解决方案 > 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。它将SydneyAdelaide进行比较,比较后将得到 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(); 现在将光标在SydneyMelbourne之间移动,再次添加Adelaide(如第一次比较)?我知道我的逻辑中缺少一些东西,因为代码实际上按字母顺序排列了所有城市(这就是代码的目的),但我在哪里犯了错误???请帮助我,在此先感谢!

标签: java

解决方案


你是对的; 直到你写下“再次 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 { ... }

推荐阅读