我必须重载 LinkedList 的 add 方法,以便它按订单号(整数)按顺序添加新的 CustomerOrders。这是我到目前为止的代码。
public boolean add(CustomerOrder order)
{
ListIterator<CustomerOrder> i = this.listIterator();
if(!(i.hasNext())) //there are no orders in the list
{
i.add(order);
return true;
}
while(i.hasNext())
{
int compare = order.compareTo(i.next(), 1);//compareTo returns 0 if the orders have the same order number, 1 if order greater order num, -1 if order has lower order num
if(compare == 0) //can't add the order if another order has the same order num
{
return false;
}
else
{
if(compare == 1) //order is greater than i.next()
{
i.add(order); //my guess is that the problem is here
return true;
}
}
}
return false;
}
当我输入订单号为 1 到 5 的订单时,列表为 1、5、4、3、2。我想要的是列表为 1、2、3、4、5。谁能指出我哪里出错并给我一些修复它的提示?