我想要做的是使用 compareTo() 方法比较removedItemFromList1 和removedItemFromList2,然后将较小的值插入modifiedList3 并将两个removedFromList 1 或2 中较大的一个放回其原始列表中。如果我有足够长的头发可以拉,它现在已经被拉了......我没有正确铸造吗?我如何正确地使用 compareTo() 方法来完成这个?
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;
}
}