Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1023115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:33:57+00:00 2026-05-16T11:33:57+00:00

I use Java. I already have a class for a custom object called Subject.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:33:58+00:00Added an answer on May 16, 2026 at 11:33 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Java 6 Collecetions API. I need a collection that should have only
I'm developing (another) java web framework for personal use, and in addition to that
I use java 7, and I create a varargs method public class JavaApplicationTest {
i use java reflections to get methods used in a class.I also want to
I am trying to use Java's useDelimiter method on it's Scanner class to do
I have been attempting to enhance my GUI system written in Java to use
I have a virtual keyboard class which implements a QWERTY alphanumeric only on-screen keyboard
Suppose I have a class called Foo . This class will be modified by
My situation is following. I have one Activity that contains three main parts. At
I have a class that is something like the following: public class Foo {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.