This discussion is archived
3 Replies Latest reply: May 26, 2011 1:08 AM by Tolls RSS

Generics and Casting

864441 Newbie
Currently Being Moderated
Hi,

I am just returning to programming after a two year hiatus, so please forgive both my ignorance and programming style. Anyway, generics are new to me but Java is not really. I'm starting to learn generics. I've made two classes to implement a linked list. One is LinkedListElement, and the other the larger LinkedList class. I wrote them out pretty quickly to get my mind back into Java programming using Objects to pass data around. I decided to re-read the Java tutorial and began to learn about Generics. Great! I modified the code to use generics. Now, although the code compiles and runs properly, I am confused.

I get warnings that there are unchecked or unsafe operations in the code. I searched online for answers, but I think I am missing something important about how generics work. It looks to me like a method that I have declared to return a generic is returning an Object instead? I don't understand why? This means I have to cast the object being returned from the method, which just seems odd to me since I thought that is what generics were supposed to prevent. Again, I'm only a few days into learning generics, but it seems like when I use them I need to keep casting things. Am I doing it wrong? Here is the code for both classes. Thank you in advance for any help!

I also tried to compile with the -Xlint:unchecked option, but all that seemed to do was suppress the warning messages, I did not get any detailed information. boggle

This is the code for an individual list element: (again, sorry for the code, I'm kinda OCD about different code things and I have decades old bad habits, like assigning nulls)

I am fairly certain that the 'getNextElement' method is the one that is causing the problem, but I don't understand why exactly. That is also the method where I do not understand why I have to use a cast for the return type and why the next.getElement() method returns an Object and not the 'E' type?
public class LinkedListElement <E>  {
    E                   element = null;
    LinkedListElement   next    = null;

    public LinkedListElement() {
        element = null;
        next    = null;
    };

    public LinkedListElement( E o ) {
        element = o;
        next    = null;
    };

    public LinkedListElement( E o, LinkedListElement n ) {
        element = o;
        next    = n;
    }

    public E      getElement()      { return element; };
    public void   setElement( E o ) { element = o;    };
    
    public LinkedListElement getNext()                         { return next;    };
    public void              setNext( LinkedListElement n )    { next = n;       };


    public E getNextElement() {
        if ( next != null ) {
            return (E)next.getElement();
        }
        return null;
    }

    public void   clearElement()         { element = null; }
    public void   clearNext()            { next    = null; }


    public E swapElement( E o ) {
      E t = element;

      element = o;
      return    t;
    };
}
And finally, this is the larger LinkedList class, I get warnings from this file as well.
public class LinkedList<T> {
    LinkedListElement list = null;
    
    public LinkedList() {
        list = null;
    }

    public LinkedList( LinkedListElement o ) {
        list = o;
    }

    public LinkedList( T o ) {
        list = new LinkedListElement( o );
    }

    public LinkedListElement getFirst() {
        return list;
    }

    public void add( T o ) {
        LinkedListElement l = new LinkedListElement( o, list );
        list = l;
    }

   public LinkedListElement find( T o ) {
       LinkedListElement l = list;
       
       while ( l != null ) {
           if ( l.getElement() == o ) {
               return l;
           } else {
               l = l.getNext();
           }
       }

       return null;  // Not found
   }   

   
   public LinkedListElement findEnd() {
       LinkedListElement l = list;

       if ( l == null ) {  return null; }

       while ( l.getNext() != null ) {
            l = l.getNext();
       }

       return l;
   }


   public LinkedListElement findBefore( LinkedListElement t ) {
       LinkedListElement l = list;

       if ( l != null ) {
         while ( l != null ) {
           if ( l.getNext() == t ) {
               return l;
           } else {
               l = l.getNext();
           }
         }
       } 
   
       return null;
   }




   public void addToEnd( T o ) {
        LinkedListElement l;

        l = findEnd();

        if ( l != null ) {
            l.setNext( new LinkedListElement( o ) );
        } else {
            // No linked list exists yet, so make a new one
            list = new LinkedListElement( o );
        }
   }


   public void remove( T o ) {

       LinkedListElement l;
       LinkedListElement b;

       l = find( o );

       if ( l != null ) {
            b = findBefore( l );

            if ( l == list ) {
                list = l.getNext();
                return;
            }

            if ( b != null ) {
                b.setNext( l.getNext() );
            }
       }
   }


}
Thank you again,

-gene
  • 1. Re: Generics and Casting
    796440 Guru
    Currently Being Moderated
    Not gonna read your code in detail, but a couple things look wrong right off the bat:
    public class LinkedListElement <E>  {
        E                   element = null;
    
        // should be LinkedListElement<E> next;
        LinkedListElement   next    = null;
     
    // ... snip ...
    
    
        // should be public LinkedListElement<E> getNext() { ... }
        public LinkedListElement getNext()                         { return next;    };
    I didn't read any further, but I assume there are more like that.

    Also note that initializing member variables to null is pointless, and in some shops (like any shop where I have any say) is considered bad practice.

    Also note that if you want to see an actual LinkedList implementation using generics to compare to yours, the source for java.util.LinkedList (as for all the core API classes) is in src.zip in the JDK that you downloaded.
  • 2. Re: Generics and Casting
    DrClap Expert
    Currently Being Moderated
    Apparently you haven't ever looked at an example of code which uses generics. Let me just say that this line
    LinkedListElement   next    = null;
    needs to use generics like this:
    LinkedListElement<E> next    = null;
    and the same goes for a good many more declarations in your code. Including method declarations.
  • 3. Re: Generics and Casting
    Tolls Journeyer
    Currently Being Moderated
    On top of that this:
    public class LinkedList<T> {
        LinkedListElement list = null;
    should presumably be:
    public class LinkedList<T> {
        LinkedListElement<T> list = null;
    ?
    Along with all the other references to LinkedListElement in LinkedList.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points