I am trying to add the integer elements of two Linked Lists. How do I add the contents of two elements of a linked list.

For example:

public void main {
........
LinkList newList = new LinkList();
.........
Link link1, link2, linkAns;

......
......

link2 = newList.deleteFirst();
link1 = newList.deleteFirst();

linkAns = link1 + link2; (ERR = operator + cannot be applied to Link, Link)

Any help would b greatly appreciated

Well, "...." doesn't give much insight into what you are doing with your lists, but to perform math operations on the elements you will need to cast them to Integer types. If you are using Java 1.5 or 1.6, auto-boxing will let you add them directly. Otherwise you will need to use Integer.intValue() to convert the Integer objects to int primaries and then add them.

Nor can we know how your "LinkList" class works.
The standard LinkedList class has a very nice addAll method to add all elements from one List to another.

If your "LinkList" (stupid idea, never reimplement something available in the standard libraries) implements List it would have such a method too.

Thank you both for the response. I figured it out some other way. I actually used a stack implementation of linked list. I will try what you both mentioned to see if I can figure that out as well.

Ezzaral, thank you for your continuous tips when I ask questions. I appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.