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

  • Home
  • SEARCH
  • 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 7190249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:28:24+00:00 2026-05-28T19:28:24+00:00

Hi I am writing a linked list data type. I have an inner class

  • 0

Hi I am writing a linked list data type. I have an inner class node that I use to store the elements and the successors. I am currently having trouble with my getElement in my node and my get method in my list.
this is my getElement in the node

public E getElement(){
  return this.element;
}

where element is an instance variable declared by E element. However when I try to return it in my gets method like this

    public E get(int index){
    Node current;
    if(index < 0 || index >= size)
      throw new IndexOutOfBoundsException();
    if(index == 0)
      return head.getElement();
    else{
      current = head;
      for(int i = 0; i< index; i++){

        current = current.getSuccessor();
      }
      return current.getElement();
    }
  }

I get the error cannot convert from object to type E. I can hack around it and type cast it to an E but I feel like there is some underlying thing about generics that I am missing. If you’ve guess that this is for homework you are correct and thank you 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-05-28T19:28:26+00:00Added an answer on May 28, 2026 at 7:28 pm

    You probably want Node to be generic too, so you’d have

    public E get(int index){
      if(index < 0 || index >= size)
        throw new IndexOutOfBoundsException();
    
      Node<E> current = head;
      for(int i = 0; i < index; i++) {
        current = current.getSuccessor();
      }
      return current.getElement();
    }
    

    (I’ve simplified your code a little at the same time. In particular, it’s a good idea to declare variables at the point at which you actually need them.)

    Node<E> would look like this:

    class Node<E> {
        private final E element;
        private Node<E> successor;
    
        public Node(E element) {
            this.element = element;
        }
    
        // etc
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently writing stack that is being implemented with a linked list. I
I am writing a class that is linked to an external resource. One of
For a CS class I am writing a linked list implementation of a linked
I'm writing a function in C that takes in a linked list and a
I'm writing a linked list and I want a struct's destructor (a Node struct)
I am writing a program in c++ which implements a doubly-linked list that holds
I'm currently writing a linked list and trying to free up memory allocations when
I'm writing an immutable linked list class in C, but one method is mysteriously
I'm writing a simple linked list based memory manager in the form: ...Header|Block|Header|Block... with
I am writing an application that is linked to Active Directory, and I need

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.