What I want to do is use the compareTo() method to compare removedItemFromList1 and removedItemFromList2, then insert whichever value is smaller into the modifiedList3 and put the larger of the two removedFromList 1 or 2 back into its original list. If I had hair long enough to pull, it would have been pulled out by now...Am I not casting correctly? How do I correctly go about using the compareTo() method to accomplish this?
public class List<Integer> implements Comparable
{
private ListNode<Integer> firstNode;
private ListNode<Integer> lastNode;
public void insertAtBack (Integer insertItem)
{
if ( isEmpty())
firstNode = lastNode = new ListNode<Integer>(insertItem);
else
lastNode = lastNode.nextBasket = new ListNode<Integer>( insertItem );
}
public Integer removeFromBack()
{
Integer removedItem = (Integer) lastNode.topBucketInBasket;
if ( firstNode == lastNode)
firstNode = lastNode = null;
else
{
ListNode<Integer> current = firstNode;
while ( current.nextBasket != lastNode)
current = current.nextBasket;
lastNode = current;
current.nextBasket = null;
}
return removedItem;
}
public boolean isEmpty()
{
return firstNode == null;
}
public List<Integer> merge(List<Integer> list1, List<Integer> list2)
{
List<Integer> modifiedList3 = new List<Integer>();
Integer removedItemFromList1 = (Integer) list1.removeFromBack();
Integer removedItemFromList2 = (Integer) list2.removeFromBack();
((Comparable) removedItemFromList1).compareTo( removedItemFromList2);
int comparison = compareTo(removedItemFromList2);
if ( comparison == 1)
modifiedList3.insertAtBack(removedItemFromList2);
list1.insertAtBack(removedItemFromList1);
if ( comparison == -1)
modifiedList3.insertAtBack(removedItemFromList1);
list2.insertAtBack(removedItemFromList2);
return modifiedList3;
}
@Override
public int compareTo(Integer itemToCompare)
{
final int BEFORE = -1;
final int AFTER = 1;
if (this.removedItemFromList1 < list2.removedItemFromList2) return BEFORE;
if (this.removedItemFromList1 > list2.removedItemFromList2) return AFTER;
}
}