简体   繁体   中英

Get Java's list iterator to return something other than Object

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.

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.

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>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM