This topic is 3277 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Ok, so I am suppose to create two linked lists of int and then write a method to find the common numbers in the two lists. My problem is comparing two objects of the list.. how do I do that?!? I might have to override the equals function but how do I do that for int? Here's my code so far.. I am trying to get if (l1.get(2)==l2.get(2)) to work!
package structures;

private Node tail;
private int size;

/** Creates a new instance of LinkedList */
{
tail = null;
size = 0;
}

{
size++;
{
return;
}
Node last = new Node(o);
if(tail==null)
else
tail.next = last;
tail = last;
}

public Object get(int i)
{
assert(i>=0 && i<size):"Wrong parameters.";
while(i!=0)
{
n=n.next;
i--;
}
return n;
}
public String toString()
{
String s = "";
while(n!=null)
{
s = s + n.toString();
n = n.next;
}
return s;
}

private class Node
{
public Object data;
public Node next;
public Node(Object o)
{
data = o;
next=null;
}

public String toString()
{
return data.toString();
}

}

}

public class Main
{
public static void main(String[] args)
{
// TODO code application logic here

System.out.println(l1);
System.out.println(l2);

if (l1.get(2)==l2.get(2))
{
System.out.println ("Works!");
}
}
}

Thank you!!!

Share on other sites
I may be wrong, but shouldn't you be doing
return n.data;

return n;

in the get method? I think right now what you are comparing are the nodes, instead of the data inside the nodes...

Share on other sites
Also, in Java i think you need to use .equals:

if ( l1.get( 2 ).equals( l2.get( 2 ) ) )

Share on other sites
Quote:
 Original post by musafir2007My problem is comparing two objects of the list.. how do I do that?!? I might have to override the equals function but how do I do that for int?Here's my code so far.. I am trying to get if (l1.get(2)==l2.get(2)) to work!*** Source Snippet Removed ****** Source Snippet Removed ***Thank you!!!

Since the data in the Node class is stored as an Object type, just call l1.data.equals(l2.data). Java primitive types will cast to their object types (int -> Integer) when you assign them to an object type. <-- Sorry, that won't work due to having to cast the returned Node object.

As a side note, you might think about returning n.data from your get method (and modifying the code surrounding the get calls). Most LinkedList implementations will return the data associated with a node; they don't return a reference to the node itself which allows the user of your LinkedList to start mucking around with the internal node pointers defeating the purpose of Encapsulation

Actually, yeah, you need to do the suggestion above anyway. After re-looking at your code I realized what problem you're having.

You're returning a Node type from the get method, which means you're trying to compare one node type to another. You either need to add an equals() method to your node class (not recommended) or return the actual data in the node from your get method (recommended).

Currently, to compare the data returned from the get() method you would need cast the object returned from get() to a Node type (which requires your Node type to be public) and then access the data on the node type like this:

// The Node class also must be public... Again, NOT RECOMMENDED!  Just change get() to return the data itself.if (((LinkedList.Node)l1.get(2)).data.equals(((LinkedList.Node)l2.get(2)).data))

[Edited by - CaspianB on June 1, 2009 8:31:23 AM]

Share on other sites
Is there any particular reason you are rolling your own list, as opposed to using a java.util.LinkedList<Integer>? The only reason I can think of is homework, and we aren't supposed to help much with that [wink]

Share on other sites
Yeah, I'd assume it is homework. However, pointing out errors in displayed code is a far cry from showing working code when all they've done is copy/paste the homework question at hand. One is acceptable, the other is not. :)

• 23
• 10
• 19
• 15
• 14