Going backwards in a ListIterator WITHOUT duplicating the last element that was shown

Viewed 64

I've been learning Java from scratch and really love it but there is this little problem of mine here I can't wrap my head around it. The following code works, I've tested it and the point is when the user goes backward in the list, the last output should not be duplicated. And here comes my question: why is (!goingForward) points to .next and not the other way around?

public static void visit(LinkedList<String> linkedList) {

    ListIterator<String> listIterator = linkedList.listIterator();
    boolean quit = false;
    boolean goingForward = true;
    Scanner scanner = new Scanner(System.in);

    if(linkedList.isEmpty()) {
        System.out.println("No cities to visit");
    } else {
        printMenu();
    }
    while (!quit) {
        int actions = scanner.nextInt();
        scanner.nextLine();
        switch (actions) {
            case 0:
                printMenu();
                break;
            case 1:
                if(!goingForward) {
                    if(listIterator.hasNext()) {
                        listIterator.next();
                    }
                goingForward = true;

                }
                if(listIterator.hasNext()) {
                    System.out.println("Now visiting: " + listIterator.next());
                } else {
                    System.out.println("Reached the end of the list");
                    goingForward = false;
                }
                break;
            case 2:
                if(goingForward) {
                    if(listIterator.hasPrevious()) {
                        listIterator.previous();
                    }
                    goingForward = false;
                }
                if(listIterator.hasPrevious()) {
                    System.out.println("Now visiting: " + listIterator.previous());
                } else {
                    System.out.println("We are at the start of the list");
                    goingForward = true;
                }
                break;
            case 3:
                System.out.println("Thanks for using the app");
                quit = true;
                break;
        }

    }
1 Answers

It's to avoid visiting the last city again when changing direction. Try what happens when you remove that logic:

public class ListIteratorDemo {
  public static void main(String args[]) throws Exception {
    LinkedList<String> cities = new LinkedList<>();
    cities.add("A");
    cities.add("B");
    cities.add("C");
    visit(cities);
  }

  public static void visit(LinkedList<String> linkedList) {

    ListIterator<String> listIterator = linkedList.listIterator();
    boolean quit = false;
    //    boolean goingForward = true;
    Scanner scanner = new Scanner(System.in);

    if(linkedList.isEmpty()) {
      System.out.println("No cities to visit");
    }
    else {
      printMenu();
    }

    while(!quit) {
      int actions = scanner.nextInt();
      scanner.nextLine();
      switch(actions) {
        case 0: {
          printMenu();
          break;
        }
        case 1: {
          //          if(!goingForward) {
          //            if(listIterator.hasNext()) {
          //              listIterator.next();
          //            }
          //            goingForward = true;
          //          }

          if(listIterator.hasNext()) {
            System.out.println("Now visiting: " + listIterator.next());
          }
          else {
            System.out.println("Reached the end of the list");
            //            goingForward = false;
          }
          break;
        }
        case 2: {
          //          if(goingForward) {
          //            if(listIterator.hasPrevious()) {
          //              listIterator.previous();
          //            }
          //            goingForward = false;
          //          }

          if(listIterator.hasPrevious()) {
            System.out.println("Now visiting: " + listIterator.previous());
          }
          else {
            System.out.println("We are at the start of the list");
            //            goingForward = true;
          }
          break;
        }
        case 3: {
          System.out.println("Thanks for using the app");
          quit = true;
          break;
        }
      }
    }
  }

  private static void printMenu() {
    System.out.println("0=menu, 1=next, 2=previous, 3=quit");
  }
}

I suppose it is to teach about the ListIterator cursors positions:

enter image description here

About your question: If you type 1 (forward) and were previously going backwards (!goingForward), the cursor will be to the left of the last visitied city. It has to be shifted to the right of the last visited city in order to avoid visiting it again when moving forward (calling next).

Related