The function 'addInOrder' is supposed to add String elements (names of Australian cities) in alphabetical order to a LinkedList 'placesToVisit'.
And according to the tutorial I was following, 'addInOrder' function actually worked as intended. However, the function was written in a way to return a boolean, which is private static boolean addInOrder(LinkedList<String> linkedList, String newCity) {}
.
But I wanted to experiment and decided to write the function 'addInOrder' to return void instead, which is private static void addInOrder(LinkedList<String> linkedList, String newCity) {}
.
But the output did not return in alphabetical order and there were even duplicates despite the use of 'break' keyword in the while loop.
Original code according to the tutorial was:
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) {
System.out.println(newCity + " already listed as destination.");
return false;
} else if (comparison > 0) {
stringListIterator.previous();
stringListIterator.add(newCity);
return true;
}
}
stringListIterator.add(newCity);
return true;
}
Output:
Now visiting, Adelaide
Now visiting, Brisbane
Now visiting, Canberra
Now visiting, Darwin
Now visiting, Melbourne
Now visiting, Perth
Now visiting, Sydney
My code:
public class Demo {
public static void main(String[] args) {
LinkedList<String> placesToVisit = new LinkedList<>();
addInOrder(placesToVisit, "Sydney");
addInOrder(placesToVisit, "Melbourne");
addInOrder(placesToVisit, "Brisbane");
addInOrder(placesToVisit, "Perth");
addInOrder(placesToVisit, "Canberra");
addInOrder(placesToVisit, "Adelaide");
addInOrder(placesToVisit, "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 void addInOrder(LinkedList<String> linkedList, String newCity) {
ListIterator<String> stringListIterator = linkedList.listIterator();
if (linkedList.size() == 0) {
linkedList.add(newCity);
} else {
while (stringListIterator.hasNext()) {
int comparison = stringListIterator.next().compareTo(newCity);
if (comparison == 0) {
System.out.println(newCity + " already listed as destination.");
break;
} else if (comparison > 0) {
stringListIterator.previous();
stringListIterator.add(newCity);
break;
} else if (comparison < 0) {
stringListIterator.add(newCity);
break;
}
}
linkedList.addLast(newCity);
}
}
}
Output:
Now visiting, Adelaide
Now visiting, Darwin
Now visiting, Brisbane
Now visiting, Canberra
Now visiting, Perth
Now visiting, Melbourne
Now visiting, Sydney
Now visiting, Melbourne
Now visiting, Brisbane
Now visiting, Perth
Now visiting, Canberra
Now visiting, Adelaide
Now visiting, Darwin
You are adding each city twice. First you are adding it in the loop just before break
. Then you are adding it at the end of the list after the loop, in this line:
linkedList.addLast(newCity);
You will also notice that the last half of your output is exactly your cities in the order you inserted them.
break
is weaker than return
. break
continues execution after the loop while return
exits the method completely.
Edit: You've got one further problem: you are not adding in order. If the list is non-empty, your if
/ else
chain will always find one true case. This means that unless the city to be added is already first in the list(!), you will add it either before or after the first element, no later.
As an aside, for production code I would prefer to use ArrayList
rather than LinkedList
. If there were 100 000 cities insertion in order into the ArrayList
might become prohibitively expensive. If so, I would just insert at the end and sort the list after all cities had been added.
After corrections posted by the contributor (Ole VV) : "break is weaker than return. break continues execution after the loop while return exits the method completely."
I reviewed the code:
private static void addInOrder(LinkedList<String> linkedList, String newCity) {
ListIterator<String> stringListIterator = linkedList.listIterator();
if (linkedList.size() != 0) {
while (stringListIterator.hasNext()) {
int comparison = stringListIterator.next().compareTo(newCity);
if (comparison == 0) {
System.out.println(newCity + " already listed as destination.");
return; //return keyword is used instead of break keyword to exit the method
} else if (comparison > 0) {
stringListIterator.previous();
stringListIterator.add(newCity);
return; //return keyword is used instead of break keyword to exit the method
}
}
}
stringListIterator.add(newCity);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.