Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 300
Joshua Avatar asked Aug 28 '10 07:08

Joshua


2 Answers

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.

like image 97
YoK Avatar answered Sep 30 '22 19:09

YoK


You can use genericity with your ListIterator.

private ListIterator<Subject> itr;

Now itr.next() return subjects.

Resources :

  • Oracle's tutorial on Collections - Iterator

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

like image 28
Colin Hebert Avatar answered Sep 30 '22 18:09

Colin Hebert