0

I have two fields in my custom class List:

  • LinkedList<String> breadList;
  • Iterator<String> iterator.

And I've defined two methods:

  • showBreadList() - for displaying the contents of the list;
  • addInOrder() - for inserting a new element into the list (the list should be updated in such a way so that elements are maintained in sorted order).

My code:

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();
        
    }
}

The list gets populated in the main() method via addInOrder().

Expected output:

Values in breadList:  
Ciabatta  
Flatbread  
Sourdough  
Wheat Bread  
White Bread

Actual output:

Value in breadList:  

I.e. I'm getting nothing. Can anybody give a hint on what I'm doing wrong?

0

1 Answer 1

2

Seems like the goal of your assignment is to learn how to deal with a ListIterator.

The core thing to understand about the Iterators 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().

Here's a quote from the official tutorial Provided by Oracle:

Intuitively speaking, the cursor is always between two elements — the one that would be returned by a call to previous and the one that would be returned by a call to next. The n+1 valid index values correspond to the n+1 gaps between elements, from the gap before the first element to the gap after the last one. The following figure shows the five possible cursor positions in a list containing four elements.

enter image description here

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
Sign up to request clarification or add additional context in comments.

1 Comment

I'm learning Java on my own through online courses, it wasn't an assignment but does look like one. Yes, I wanted to figure out how LinkedList & iterator works! That I didn't put break in the loop was also an issue too, I think. It helped me a lot. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.