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にすることです。誰かが私がどこで間違っていたかを指摘し、それを修正するためのいくつかのヒントを教えてもらえますか?