Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-09-2007, 06:56 PM
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
Operator < cannot be applied to java.lang.Object, Object
Hi,I am making a program with a linked list and nodes and i am trying to use the generic templates.
This is an example of what i put to define my class 'public class LinkedList<Object>' and then put Object for the type throughout the code. So far it has worked for the Node class and now i am trying to get it to work on the LinkedList class.
The problem i am having is i am comparing to Object items for a sorting method to put the linked list in order.

Code:
public void insertion(Object grad, Object y_int) { boolean found = false; current = head_ptr; while (found == false && current != sentinel) { if (numberOfNodes == 0) { //std::cout << "in insertion numbernodes = 0" << endl; addToTail(grad, y_int); found = true; } else { if (current.get_Data_x() == grad) { if (current.get_Data_y() < y_int) --------- this is the line that has the problem.
I get the error 'operator < cannot be applied to java.lang.Object, Object'.
Can anyone tell me how i can fix this problem or know what it could be?

Thanks
Albert
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-13-2007, 04:19 PM
Member
 
Join Date: Jul 2007
Posts: 44
susan is on a distinguished road
Generics is a powerful tool in Java. But I'm wondering why you're creating a linked list with an Object as the generic.
My suggestion is that you call the generic in the standard Java way and that is with an uppercase T

Code:
public class LinkedList<T> { public T doSomething(T c) { return c; } }
Or at the least, make it a Linked List of Nodes. But I digress.

Let us look at your code. I'm unsure what is returned with the method invocation get_Data_y(), but I am going to assume it's of some Object type. If that's the case you can not use a mathmatical notation such as < > == != etc on it.

Now I know you're asking, but == worked on the above. Well it sort of did. It worked in the sense that is it comparing the two pointers. But they aren't checking to see if the values are the same. This is why with Objects (including the class String) you have to use an equals method of some type.

Unfortionatly since you are using the base class of Object you don't have the nifty method compareTo like other classes usually do. All you have use of is equals:

Object (Java 2 Platform SE 5.0)

Anyways, If this isn't the problem, then please post your Node Class so that we can see if we can debug the problem from there.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Data Object (basics) JavaForums Java Blogs 0 03-07-2008 05:16 PM
name clash: equals(E) in and equals(java.lang.Object) AdRock New To Java 0 01-26-2008 12:13 AM
size of a java object ravian New To Java 0 11-12-2007 10:19 AM
Error: cannot be applied to (java.lang.String) carl New To Java 1 08-05-2007 07:33 AM
Creating object of Type Object class venkatv New To Java 3 07-17-2007 04:33 PM


All times are GMT +3. The time now is 01:13 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org