d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Explain This To Me Plz
Add Reply New Topic New Poll
Member
Posts: 31,292
Joined: Mar 25 2009
Gold: 0.00
Feb 6 2019 09:35pm
Code
package com.company;

import javax.swing.text.html.HTMLDocument;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;

public class Demo {

public static void main(String[] args) {

LinkedList<String> placesToVisit = new LinkedList<String>();
addInOrder(placesToVisit, "Sydney");
addInOrder(placesToVisit, "Melbourne");
addInOrder(placesToVisit, "Brisbane");
addInOrder(placesToVisit, "Perth");
addInOrder(placesToVisit, "Canberra");
addInOrder(placesToVisit, "Adelaide");
addInOrder(placesToVisit, "Darwin");


printList(placesToVisit);

addInOrder(placesToVisit, "Alice Springs");
addInOrder(placesToVisit, "Darwin");
printList(placesToVisit);



visit(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
// Brisbane --> Adelaide
stringListIterator.previous();
stringListIterator.add(newCity);
return true;
} else if (comparison < 0) {
// move on to next city

}
}

stringListIterator.add(newCity);
return true;

}


private static void visit(LinkedList cities) {
Scanner scanner = new Scanner(System.in);
boolean quit = false;
boolean goingForward = true;
ListIterator<String> listIterator = cities.listIterator();

if (cities.getFirst() == "") {
System.out.println("No cities in the itenerary");
return;
} else {
System.out.println("Now visiting " + listIterator.next());
printMenu();
}

while (!quit) {
int action = scanner.nextInt();
scanner.nextLine();
switch (action) {
case 0:
System.out.println("Holiday (vacation) over");
quit = true;
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:
printMenu();
break;
}
}
}


private static void printMenu() {
System.out.println("Available actions:\npress ");
System.out.println("0 - to quit\n" +
"1 - go to next city\n" +
"2 - go to previous city\n" +
"3 - print menu options");
}


}





What i'm confused about are these lines of code:

Code
stringListIterator.previous();
stringListIterator.add(newCity);




How are you adding newcity to linkedlist, don't you normally have to add using linkedlists name + .add? for example: placestovisit.add(newcity)











Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 7 2019 10:36am
Quote (ferf @ Feb 6 2019 10:35pm)

What i'm confused about are these lines of code:

Code
stringListIterator.previous();
stringListIterator.add(newCity);




How are you adding newcity to linkedlist, don't you normally have to add using linkedlists name + .add? for example: placestovisit.add(newcity)


see the documentation for ListIterator

https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html#add(E)
Member
Posts: 7
Joined: Jun 7 2010
Gold: 0.00
Feb 16 2019 07:07am
If you use "add" on an iterator, the item will be added AFTER the iterator. That method "addInOrder" is basically running thru your (already sorted list) with that iterator, checking if the current city's name is lexicographically "after" the item that you want to add. If that's the case, you need to move to your previous items since you need to add the item before the iterator.
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Feb 16 2019 04:35pm
Quote (ImRickJames @ Feb 16 2019 08:07am)
If you use "add" on an iterator, the item will be added AFTER the iterator. That method "addInOrder" is basically running thru your (already sorted list) with that iterator, checking if the current city's name is lexicographically "after" the item that you want to add. If that's the case, you need to move to your previous items since you need to add the item before the iterator.


Stop encouraging him. He needs to learn HOW TO LEARN
Member
Posts: 7
Joined: Jun 7 2010
Gold: 0.00
Feb 16 2019 05:36pm
Quote (waraholic @ Feb 17 2019 12:35am)
Stop encouraging him. He needs to learn HOW TO LEARN


Just noticed this guy has like 10 open threads on Java lmao.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 16 2019 09:57pm
Quote (ImRickJames @ Feb 16 2019 06:36pm)
Just noticed this guy has like 10 open threads on Java lmao.


he's been asking basic java questions for years. nothing wrong with trying to learn.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll