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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:52:23+00:00 2026-06-09T12:52:23+00:00

[SOLVED] I’m a C++ guy learning Java for (hopefully) an upcoming job. As such

  • 0

[SOLVED]

I’m a C++ guy learning Java for (hopefully) an upcoming job. As such I’m practicing implementing a Linked List in Java on my own. I finished implementing a standard doubly linked list and it’s working great. However, I then tried to create an Ordered Linked List and realized the inability to overload the ‘<‘ operator was going to be a severe problem.

I’ve looked through other people’s questions on this site but it still isn’t getting through to me, so I thought I’d post my code and get an answered more tailored to what I’m doing.

Onwards..

The Linked List Class:

public class MyList<T>  {

/*** ~Public Interface~ ***/
       //insert, delete, size, print, etc.
       ...

    /*** Private Data Members ***/
       //node begin, end, T data
   ....


/** Private node class **/
//Represents the nodes in the list
private class node implements Comparable<T>{ //Don't know if this is right
    node next;
    node prev;
    T    data;

    node(node p, node n, T d){
        next = n;
        prev = p;
        data = d;
    }

    @Override
    public int compareTo(T o) {
        return (data <= o ? 1 : 0); //Get an error still here
    }

          /** Iterator **/
               //iterator class
}

The Ordered Linked List Class:

public class OrderedList<T> extends MyList<T> implements Comparable<T>{ //Pretty sure this is wrong

public void insert(T d){
    if(empty()){
        push_front(d);
    } else {
        MyList<T>.MyListIter it = begin();
        int i = 0;
             //This won't work obviously
        for(; i < size() || it.current().compareTo(it.next()) == -1; ++i, it.next()){
                    //find node to place the new node before it 
                        it.prev() //Need to go back one since we went forward in the loopcheck.


        }
    }
}

Really at a loss here. How does one achieve something similar to operator overloading so that I can finish this ordered list implementation?

I’m also trying to get a grasp on inheritance in Java, so if you see something wrong in that regard, feel free to chime in about that too.

Thanks all.

UPDATE:

Ok, I made the changes I thought I had to make but I’m still running into errors. Here’s the new code:

The node class within the MyList class:

private static class node<T> implements Comparable<T>{
    node<T> next;
    node<T> prev;
    T    data;

    node(node<T> p, node<T> n, T d){
        next = n;
        prev = p;
        data = d;
    }

    public int compareTo(T o) {
        return ((Comparable<T>) this.data).compareTo(o);
    }

}

Ordered List:

public class OrderedList> extends MyList{

Usage of compareTo:

if(((Comparable<T>) it).compareTo(it.next()) == -1) found = true;

The compiler forced me to do those casts and now the error is:

MyList$MyListIter cannot be cast to java.lang.Comparable

List iterator class looks like:

public class MyListIter{

And it’s inside MyList.

EDIT [SOLVED]

When I was using my iterator, instead of doing it.current() to access the actual data, i was doing it.compareTo(..), but of course my iterator isn’t the data and thus doesn’t know anything about compareTo().

  • 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-09T12:52:25+00:00Added an answer on June 9, 2026 at 12:52 pm

    As you stated, you cannot overload operators in Java. This means that your sorted list cannot ever use <= to compare objects of any kind. Rather, you need to rely on the data object to have a compareTo() method or provide a way for the user of your list to give a Comparator object. If you wish to strictly limit your list to the first case, you can declare it as

    public class OrderedList<T implements Comparable<T>> extends MyList<T>
    

    This will guarantee that the objects inserted into your list have a compareTo() method. Now your Node.compareTo() function simply delgates the comparison to this method:

    public int compareTo(T o) {  
        return this.data.compareTo(o.data);
    }
    

    Allowing custom Comparators can get a little tricky. I suggest you leave that for after you get a default comparison to work.

    p.s. Do you want to be able to compare lists to each other? If so, then having OrderedList implement Comparable makes sense. However, you might want to leave this for a later exercise after you have your list class working with its basic operations.

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

Sidebar

Related Questions

[SOLVED] So I decided to try and create a sorted doubly linked skip list...
SOLVED: It was because each of my service classes had their own context associated
SOLVED - see Bish below I've got a list of checkboxes that all dump
[SOLVED]: Applying proper list iteration procedure fixed problem. (Shown below) I currently have a
SOLVED! to get coordinates of the sprites frame use sprite.boundingBox.origin.x; Hello! I am implementing
SOLVED I'm writing a code for making Set by linked lists in C++ with
SOLVED MY OWN QUESTION! THANKS EVERYONE FOR THE HELP :) Ok. I'm having trouble
(Solved already, I'm writing this for the next guy) I was running git daemon
Solved! Finished CSS and HTML at bottom. Ok, so here is the situation. I
** SOLVED ** I'm fairly new to Java and so far I love it!

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.