views:

57

answers:

2

I use Java.

I already have a class for a custom object called "Subject".

I have another class that only contains a Linked List of Subject objects. (called subjectsList)

I wrote one of the methods (called "getSubject()") in the subjectsList class that is meant to return the Subject object at a specific index in the Linked List. To do this, I used a ListIterator. I would create a ListIterator at a given index in the LinkedList and use the .next() method.

However, the .next() method in a ListIterator only returns a java.lang.Object, even though I want it to return a Subject object.

How do I get ListIterator.next() to return a Subject object?

I've tried downcasting to a Subject but this seems to fail, and I read somewhere that you can't downcast unrelated objects.

This is my subjectsList class:

import java.util.*;
public class subjectsList
{
    //my LinkedList of Subject objects and a ListIterator
    private LinkedList<Subject> l;
    private ListIterator itr;

    //constructor that simply creates a new empty LinkedList
    public subjectsList()
    {
        l = new LinkedList();
    }

    //Method to return the Subject object at a specific index in the LinkedList
    public Subject getSubject( byte index )
    {
        itr = l.listIterator( index );
        return itr.next();
    }

    //Method to add a new Subject object to the LinkedList
    public void addSubject( String nameInput, byte levelInput )
    {
        l.addLast( new Subject( nameInput, levelInput ) );
    }

    //Method to remove the Subject object at a specific index in the LinkedList
    public void removeSubject( byte index )
    {
        itr = l.listIterator( index );
        itr.next();
        itr.remove();
    }
}

The method in question is the "getSubject()" method.

+4  A: 

You need to declare your iterator as :

private ListIterator<Subject> itr;

Also create your LinkedList object as follows in constructor:

l = new LinkedList<Subject>();

You could also cast it to Subject while returning as your list only stores Subject object. But it would be better to define Iterator as said earlier.

public Subject getSubject( byte index )
{
    itr = l.listIterator( index );
    return (Subject)itr.next();
}

Other than your problem I see naming convention problem in your code. It is better to follow it and make a habbit.

NOTE: I have made changes to my answer after trying your example in Eclipse IDE. I would suggest you to use IDE as it would also show you warning about such things.

YoK
Thanks! that solved the problem.
Joshua
In java it is the convention to start class names with a capital letter. Also you need to accept answers, This improves you acceptance rate which actually motivate people to answer your questions quickly :).
YoK
fine you already accepted answer
YoK
Thanks for pointing it out...I didn't know that naming conventions were so important >.<Changed class name now.
Joshua
+4  A: 

You can use genericity with your ListIterator.

private ListIterator<Subject> itr;

Now itr.next() return subjects.

Resources :

PS: I suggest that you change the instantiation of your LinkedList into l = new LinkedList<Subject>();

Colin Hebert
It is also worth noting that a lot of Java compilers will produce a warning for `private ListIterator itr;` about using raw types.
Stephen C
Oh forgot about the <Subject> in the constructor...Thanks!
Joshua