Home > Software engineering >  How does NoSuchElementException work within this OrderedSet/OrderedList to where it wont infinitely
How does NoSuchElementException work within this OrderedSet/OrderedList to where it wont infinitely

Time:09-27

I have been working on this code for hours how can I throw a NoSuchElement exception so that it stops the loop when it reaches the beginning of the list right now here is what the test case outputs with my code

expected: but was: Expected :a Actual :z


  * Returns the data from next element in the set.
     * @return the data from next element in the set
     */
    public E next() {
        // TODO : Implement

        //returns the next element in the collection and advances
        // the iterator by one location (updating both internal references).
        // If one were to call next(), then prev(), then next(), the results of the two next() calls should be identical.
        E temp = this.nextElem.info;

        if (this.nextElem != null) {
            //throw new NoSuchElementException("No element found");
            return temp;
        }
        if (temp != nextElem.info) {
            //E temp2 = this.nextElem.info;
            //nextElem = nextElem.next;
            //return temp2;
            throw new NoSuchElementException();
        } else {
            return temp;
        }

code

CodePudding user response:

What about just doing the following?

public E next() {
    E temp = this.nextElem;

    if (temp != null) {
        return temp.info;
    } else {
        throw new NoSuchElementException();
    }
}

Edit: Sorry, I forgot to explain what went wrong in your code.

  1. What exactly will be null when you reach the end of the loop? Will this.nextElem be null? Because if so, you will already get a NullPointerException in the line E temp = this.nextElem.info;. Or, will this.nextElem.info be null?

  2. Carefully read through your code. At first, you are assigning this.nextElem.info to the variable temp. Later, you are comparing temp to nextElem.info in the if-statement. Keep in mind that this.nextElem.info and nextElem.info are usually synonymous. Hence, what you are effectively doing is the following: if (nextElem.info != nextElem.info) {. As you can see, the condition will always evaluate to falseand you will always go into theelsebranch and never reach yourthrow`.

CodePudding user response:

Consider that one of only two things can occur when the method is called:

  1. throw an exception because there is no next element
  2. return the next element and advance the cursor

So your algorithm could be summarized as:

public E next() {
  // throw an exception because there is no next element
  if there is no next element
    throw exception
  endif

  // return the next element and advance the cursor
  let info be the info at the next element
  advance the cursor to next element
  return info
}

How this is actually implemented depends on implementation details you have not provided. This appears to be a class assignment so pseudo-code should be sufficient to get you going. Note that the order of operations matters. For instance, if there is no next element then you shouldn't be trying to capture the value at the next element.

  •  Tags:  
  • java
  • Related