What is the last remove method doing in LinkedList?

Posted on 2011-10-27
Last Modified: 2012-08-14
In line 146 of the Java code below, is that simply how you refer to a method in the outer class from within the inner class? In other words, line is 146 simply calling the remove method in line 79 using this format: OuterClass.this.method( argument)?
public class MyLinkedList<AnyType> implements Iterable<AnyType> {

	private int theSize;
	private int modCount = 0;
	private Node<AnyType> beginMarker;
	private Node<AnyType> endMarker;

	private static class Node<AnyType>
		public Node( AnyType d, Node<AnyType> p, Node<AnyType> n ) {
			data = d; prev = p; next = n;}

		public AnyType data;
		public Node<AnyType>  prev; //instance of Node class inside the Node class
		public Node<AnyType>  next; //instance of Node class inside the Node class

	public MyLinkedList( ) {
		clear( );}

	public void clear( ) // Change the size of this collection to zero.
		beginMarker = new Node<AnyType>( null, null, null );
		endMarker = new Node<AnyType>( null, beginMarker, null );
       = endMarker;

		theSize = 0;

	public int size( ) { //Returns the number of items in this collection.
		return theSize;

	public boolean isEmpty( ) {
		return size( ) == 0;} // test to see if this is true

	public boolean add( AnyType x ) { //Adds an item to this collection, at the end
		add( size( ), x );
		return true;

	/* the add method adds an item to this collection, at specified position.
         Items at or after that 	position are slid one position higher */

	public void add( int idx, AnyType x ) {
		addBefore( getNode( idx), x );}

	public AnyType get( int idx ) { // Returns the item at position idx
		return getNode( idx ).data;}

	public AnyType set( int idx, AnyType newVal ) { //Changes the item at position idx
		Node<AnyType> p = getNode( idx ); //create node object
		AnyType oldVal =; //create anytype object, define it as the node's old data = newVal; //define node's data as the new value argument passed into method
		return oldVal; //return the old data, for whatever reason

	public AnyType remove( int idx ) { //Removes an item from this collection
		return	remove(getNode( idx ));} //uses idx to call the remove method below

	/* this addBefore method adds an item to this collection, at specified position p.
	Items at or after that position are slid one position higher */

        //the correct p is sent courtesy of add and getNode
	private void addBefore( Node<AnyType> p, AnyType x ) {
		Node<AnyType> newNode = new Node<AnyType>( x, p.prev, p ); //inserts new node btw = newNode; //moves back one node and redefines the next node
		p.prev = newNode; //redefines the node just prior to the right hand node

	private AnyType remove( Node<AnyType> p ) {  // Removes the object contained in Node p. = p.prev; =;

        //have to use loops in this method bc there are no arrays that allow jumping to an index
	private Node<AnyType> getNode( int idx)
		Node<AnyType> p;

		if( idx < 0 || idx > size() )
            throw new IndexOutOfBoundsException();

        if( idx < size( ) / 2 ) {
            p =;
            for( int i = 0; i < idx; i++ )
                p =;
            p = endMarker;
            for( int i = size( ); i > idx; i-- )
                p = p.prev;

		return p;

	public java.util.Iterator<AnyType> iterator( ) {
        return new LinkedListIterator( );

	private class LinkedListIterator implements java.util.Iterator<AnyType>
            private Node<AnyType> current =;
            private int expectedModCount = modCount;
            private boolean okToRemove = false;

            public boolean hasNext( ) {
		return current != endMarker;

            public AnyType next( ) //confusing
                if( modCount != expectedModCount )
                    throw new java.util.ConcurrentModificationException( );
                if( !hasNext( ) )
                    throw new java.util.NoSuchElementException( );

                AnyType nextItem =;
                current =; //advances current node one spot forward
                okToRemove = true;
                return nextItem; //returns data of old node

            public void remove( )
                if( modCount != expectedModCount )
                    throw new java.util.ConcurrentModificationException( );
                if( !okToRemove )
                    throw new IllegalStateException( );

                MyLinkedList.this.remove( current.prev );
                okToRemove = false;

Open in new window

Question by:shampouya
    LVL 31

    Accepted Solution


    Author Comment

    Separate question: In the program in the link that you just gave, is line 3 a way to treat doit() as the constructor for the TestIt class?
    public class TestIt {
      public static void main(String a[]){
         new TestIt().doit();
          output :
          Hello world!
      public void doit() {
          new InnerClass().sayHello();
      public void enclosingClassMethod(){
          System.out.println("Hello world!");
     class InnerClass {
       public void sayHello() {

    Open in new window

    LVL 37

    Assisted Solution

    No. It's not a constructor. Saying new TestIt().doit(); is basically the same as
    TestIt x = new TestIt();

    The code is just shortcutting the process.

    Author Comment

    This may be a stupid question, but in line 3 why don't they just write doit()?
    LVL 37

    Expert Comment

    You'd have to ask someone from the Java zone that one. It does look like main is inside the class so there should be a TestIt lying around in main without needing to call one.

    My guess is they did it that way to illustrate the normal way of doing it no matter if main is inside the class or not. They just put the main in the class to make the example shorter.

    Author Closing Comment


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    linearIn  challenge 23 54
    stringclean challenge 26 43
    word0 challenge 4 37
    drawing animated level bar based on numbers 3 43
    This article will show, step by step, how to integrate R code into a R Sweave document
    This is about my first experience with programming Arduino.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now