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 7924465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:46:26+00:00 2026-06-03T17:46:26+00:00

I am trying to implement a list ADT in Java.I followed a book on

  • 0

I am trying to implement a list ADT in Java.I followed a book on Data Structures by Goodrich.

I created 2 interfaces called Position:

public interface Position {
    Object element()throws InvalidPositionException;

}

and List which has method declaration of lists like first(), last(), etc.

Now, i have a class called DNode.java which implements Position and has methods to assign the previous and next elements for a particular node.

And finally, I have NodeList.java which implements the List interface.

In this, there is a insertBefore() method that takes Position and Object as input and makes changes to the list to insert the node before a particular position.

Now, this method is called from the ListMain class which is the main class.

    Object p = (Object) br.readLine();
    nl.insertAfter((Position)p, element);
    nl.display();

Basically, I have taken an input from the user in the form of a string, converted it to type Object and then converted it to type Position.
When I do so, I get a class cast exception that says

java.lang.String cannot be cast to ListPackage.Position

Can someone please tell me how to do the appropriate type casting. Or is my approach wrong and should I be doing something else?

I am new to data structures. So, any help will be appreciated.
Thank you very much in advance.

  • 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-06-03T17:46:28+00:00Added an answer on June 3, 2026 at 5:46 pm

    As the error says you are trying to say that a String is a Position, which it is not. Instead you need to create a Position from the String:

    String s = br.readLine();
    Position p = new DNode(s);
    

    And you will have to implement the logic of how to convert a String into a DNode object in the DNode class’s constructor

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

Sidebar

Related Questions

I am trying to implement the linked list data structure using java, it works
I am trying to implement a linked list for a data structures class and
I'm trying to implement my own List system in Java. the List class file
I am trying to implement a Stack in java (using the list interface: Interface
I am trying to implement cell List , and insert my data coming from
I am trying to implement a Linked List Sequence in Java however my method
I am trying to implement a sorted list. I have created the class I
I'm trying to implement a sorted list as a simple exercise in Java. To
As a pet project I'm trying to implement an immutable list data structure in
I'm trying to implement my very first hibernate sql query public List<Subjectgrouplist> getSubjectgroups() {

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.