Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello I have to try again. I have seen many examples over and got some advice he

ID: 3633892 • Letter: H

Question

Hello

I have to try again. I have seen many examples over and got some advice here and there, but I need help to complete this task.

I have been sitting for 3 days and scratched my head now to get it to work but nothing is working no matter how hard I try.

The task reads like this:

Create a method removeSubList in class LinkedList that takes two iterators as input, respectively, start and end of SubList to be removed.
Then Implement a method retreat () in class LinkedList that takes an iterator and returns an iterator pointing at the item above.


These are classes that are added with this task:


class ListNode<AnyType>
{
// Constructors
public ListNode( AnyType theElement )
{
this( theElement, null );
}
public ListNode( AnyType theElement, ListNode<AnyType> n )
{
element = theElement;
next = n;
}
public AnyType element;
public ListNode<AnyType> next;
}
public class LinkedList<AnyType>
{
/**
* Return an iterator representing the header node.
*/
public LinkedListIterator <AnyType> last ()
{
list node; p = header;
while (p.next != null) {
p = p.next;
}
return new LinkedListIterator <AnyType> (p);
}
}
}
public class LinkedListIterator<AnyType>
{
public AnyType retrieve( )
{
return isValid( ) ? current.element : null;
}
ListNode<AnyType> current; // Current position
}

Explanation / Answer

Here, you will get to know about the remove() method through the following java program. This method removes the last element returned by the iterator (optional operation) from the collection. As per every call to next, this method can only be called once. However, the iteration will continue other than by calling this method. This method will throw UnsupportedOperationException if the iterator doesn't support the remove operation and will throw IllegalStateException if after the last call to the next method, the remove method has already been called. In the program code given below, we have taken a list of array that contains 0- 10 elements. Then with the help of hasNext() method we will get next elements as output. And to remove any element from the loop, we have applied remove() method which will remove that element which gives 0 remainder in this iteration. Hence we get the following output. Here is the code of program: import java.util.*; public class remove{ public static void main (String args[]){ ArrayList arr = new ArrayList(); System.out.println("Total elements of iterator: "); for (int i = 1; i < 10; i++) arr.add(i); int count = 0; for (Iterator i = arr.iterator(); i.hasNext();){ System.out.println ("element is " + i.next()); } System.out.println("After removing : "); for (Iterator i = arr.iterator(); i.hasNext();){ count++; i.next(); if (count%4 == 0) i.remove(); } for (Iterator i = arr.iterator(); i.hasNext();){ System.out.println ("element is " + i.next()); } } } ======================================================================= Remove an element from Collection using Java Iterator Example /* Remove an element from Collection using Java Iterator Example This Java Example shows how to remove an element from underlying Collection using Java Iterator's remove method. */ import java.util.Iterator; import java.util.ArrayList; public class RemoveElementThroughIteratorExample { public static void main(String[] args) { //create an ArrayList object ArrayList aList = new ArrayList(); //populate ArrayList object aList.add("1"); aList.add("2"); aList.add("3"); aList.add("4"); aList.add("5"); System.out.println("ArrayList before removal : "); for(int i=0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote