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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:07:27+00:00 2026-06-13T08:07:27+00:00

So to allow any type object I must use generics in my code. I

  • 0

So to allow any type object I must use generics in my code.

I have rewrote this method to do so, but then when I create an object, for example Milk, it won’t let me pass it to my method.

Ether there’s something wrong with my generic revision, or Milk object I created is not good.

How should I pass my object correctly and add it to linked list?

This is a method that causes error when I insert an item:

   public void insertFirst(T dd)  // insert at front of list
   {
      Link newLink = new Link(dd);   // make new link

      if( isEmpty() )                // if empty list,
         last = newLink;             // newLink <-- last
      else
         first.previous = newLink;   // newLink <-- old first
      newLink.next = first;          // newLink --> old first
      first = newLink;               // first --> newLink
   }

This is my class I try to insert into linked list:

class Milk
{
    String brand;
    double size;
    double price;

    Milk(String a, double b, double c)
    {
        brand = a;
        size = b;
        price = c;
    }
}

This is test method to insert the data:

   public static void main(String[] args)
      {                             // make a new list
      DoublyLinkedList theList = new DoublyLinkedList();

      // this causes:
      // The method insertFirst(Comparable) in the type DoublyLinkedList is not applicable for the arguments (Milk)
      theList.insertFirst(new Milk("brand", 1, 2));      // insert at front

      theList.displayForward();     // display list forward
      theList.displayBackward();    // display list backward
      }  // end main()
   }  // end class DoublyLinkedApp

Declarations:

class Link<T extends Comparable<T>>
   {}

class DoublyLinkedList<T extends Comparable<T>>
   {}
  • 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-13T08:07:29+00:00Added an answer on June 13, 2026 at 8:07 am

    Based on your error you have defined T to be Comparable like

    public class DoublyLinkedList<T extends Comparable<T>> {
    

    but you are adding a type which is not Comparable. Its not clear why your class needs to be comparable as you don’t appear to using this in your insert method so I would drop the requirement.

    public class DoublyLinkedList<T> {
    

    If you are going to copy what a class in the JDK does, I suggest you understand how it works by reading its source.

    http://www.docjar.com/html/api/java/util/LinkedList.java.html

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

Sidebar

Related Questions

I have this code here, which is intended to allow any type of arguments:
is there any development environments that allow you to have one code base that
I have a generic class that should allow any type, primitive or otherwise. The
I want to allow any user to create a category using a text field.
While ruby does allow modifying any class at any place in the code, I
I have implemented a set datatype in javascript based in a generic object type,
I have a class AudioManager with a member of type AudioBufferList * . (This
I Know I can use Generics while defining the ArrayList to do that. But
I have a method like this. It works fine when my device is connected
I have this codes to allow users to upload csv files and the contents

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.