public class List {
class LinkedListExample {
private LinkedList<String> breadList = new LinkedList<>(); // <-- Edit from the answer: public to private
Iterator<String> iterator;
public void showBreadList() {
iterator = breadList.iterator();
System.out.println("Values in breadList: ");
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
public void addInOrder(String bread) {
ListIterator<String> stringListIterator = breadList.listIterator();
while(stringListIterator.hasNext()) {
if(stringListIterator.next().compareTo(bread) == 0) {
} else if(stringListIterator.next().compareTo(bread) > 0) {
stringListIterator.previous();
stringListIterator.add(bread);
} else if(stringListIterator.next().compareTo(bread) < 0) {
}
}
stringListIterator.add(bread); // <-- Edit: added
}
}
public static void main(String[] args) {
List list = new List();
List.LinkedListExample lle = list.new LinkedListExample();
lle.addInOrder("Ciabatta");
lle.addInOrder("Wheat Bread");
lle.addInOrder("White Bread");
lle.addInOrder("Sourdough");
lle.addInOrder("Flatbread");
lle.showBreadList();
}
}
I got LinkedList breadList, Iterator iterator(which I wanted it to be initialized when I call the showBreadList()) and 2 functions(showBreadList() and addInOrder()) in the LinkedListExample class.
I expected the result to be:
Values in breadList:
Ciabatta
Flatbread
Sourdough
Wheat Bread
White Bread
But the result was:
Value in breadList:
and nothing. Can anybody put a finger on where I went wrong?
CodePudding user response:
The problem is with your addInOrder
function. In the beginning the breadList
is empty so the condition in while loop always returns false causing nothing to get added to the list.
Alternatively, consider writing addInOrder
as below:
public void addInOrder(String bread) {
int positionToInsert = 0;
for (String s : breadList) {
if (s.compareTo(bread) < 0) {
positionToInsert ;
}
}
breadList.add(positionToInsert, bread);
}
Also, nitpick, you don't need to have an iterator as a class member and can make the list private
CodePudding user response:
Seems like the goal of your assignment is to learn how to deal with a ListIterator
.
The core thing to understand about the Iterator
s that their cursor always occupies a position in between the elements of the list (and it might also be positioned before the first element and right after the last element). And that's the position where a new element gets inserted when you invoke ListIterator.add()
.
In the method addInOrder()
you've messed around with conditions. If fact, only one condition that would check if the next
is lexicographically greater than the given string would be enough. If the next
is greater we continue iteration, if not we need to move the iterator the previous position by calling previous()
and then break from the loop.
Method add()
will be invoked on the iterator after the loop, when iterator is the right position to insert a new element.
That's how you can fix your method:
public void addInOrder(String bread) {
ListIterator<String> iterator = breadList.listIterator();
while (iterator.hasNext()) {
String next = iterator.next();
if (next.compareTo(bread) > 0) { // if the next element is lexicographically greater than `bread`, `bread` has to be inserted before it
iterator.previous(); // moving to the position where the new `bread` should be inserted
break; // breaking out from the loop
}
}
iterator.add(bread); // inserting the new kind of bread
}
Example with the sample data from the question:
LinkedListExample lle = new LinkedListExample();
lle.addInOrder("Ciabatta");
lle.addInOrder("Wheat Bread");
lle.addInOrder("White Bread");
lle.addInOrder("Sourdough");
lle.addInOrder("Flatbread");
lle.showBreadList();
Output:
Ciabatta
Flatbread
Sourdough
Wheat Bread
White Bread