2017-09-09 3 views
0

私は2つのLinkedListを持っています:newLinkedListとoldLinkedListはどちらもBIDクラスオブジェクトを含んでいます。以下は私のBIDクラスです:Javaのクラス変数に基づくユーザー定義オブジェクトの2つのLinkedListのカスタムソート

public class Bid { 

    private int quantity; 
    private double bidPrice; 

    public int getQuantity() { 
     return quantity; 
    } 

    public void setQuantity(int quantity) { 
     this.quantity = quantity; 
    } 

    public double getBidprice() { 
     return bidPrice; 
    } 

    public void setBidprice(double bidPrice) { 
     this.bidPrice = bidPrice; 
    } 
} 

今私はBIDクラスの価格変動に基づいてnewLinkedListとoldLinkedListのソートされた要素を含む新しいLinkedListlistを作成する必要があります。 もし私が両方のLinkedListで同じ価格を取得したら、私はnewLinkedList BIDクラスオブジェクトを保持し、古いものを削除しなければなりません。

つまり、新しいLinkedListにはprice変数に基づいてソートされたBidクラスオブジェクトが含まれている必要があります。

これは私の主な機能です:

public static void main(String[] args) throws InterruptedException, IOException { 

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("Enter size of linkedlist 1 "); 
    int size1 = Integer.parseInt(br.readLine()); 
    System.out.println("Enter size of linkedlist 2 "); 
    int size2 = Integer.parseInt(br.readLine()); 
    LinkedList<Bid> oldLinkedList= addElementsToList(size1); 
    LinkedList<Bid> newLinkedList= addElementsToList(size2); 

    /* 
      SORT BOTH THE LINKED LISTS HERE 
    */ 

} 

public static LinkedList<Bid> addElementsToList(int size) throws IOException { 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    LinkedList<Bid> bidList = new LinkedList<Bid>(); 

    for (int i = 0; i < size; i++) { 
     Bid bid = new Bid(); 
     System.out.println("Enter bid price of Object " + i); 
     bid.setBidprice(Double.parseDouble(br.readLine())); 
     System.out.println("Enter bid quantity of Object " + i); 
     bid.setQuantity(Integer.parseInt(br.readLine())); 
     bidList.add(bid); 
    } 
    return bidList; 
} 
+0

あなたは2つのリンクされたリストを持っています..そして、入れ子になったforループ、コンパレータなどを使用してください – emotionlessbananas

+0

あなたの質問は何ですか? – Turing85

+0

"2つのリンクされたリストを1つに統合し、最終的なリストをソートします。"しかし、LinkedListには "Bid"クラスオブジェクトが含まれています。したがって、並べ替えは「入札」クラスオブジェクトの価格変数に基づいて行われなければなりません。 –

答えて

1

は多分これはそれの価格はすでにnewListに存在する場合、あなたの希望は、oldList内の各入札のために、チェックするものです。存在する場合は何もせず、それ以外の場合はnewListに追加し、最後のnewListをソートします。あなたはそれをテストすることができます。

注:実際に2倍の2倍の価格を比較したいのかどうかはわかりません。

boolean containsSamePrice(LinkedList<Bid> list, double price) { 
     for (Bid bid : list) { 
      if (bid.getBidprice() == price) { 
       return true; 
      } 
     } 
     return false; 
    } 

LinkedList<Bid> mergeAndSort(LinkedList<Bid> newLinkedList, LinkedList<Bid> oldLinkedList) { 
    for (Bid oldBid : oldLinkedList) { 
     if (!containsSamePrice(newLinkedList, oldBid.getBidprice())) { 
      newLinkedList.add(oldBid); 
     } 
    } 
    Comparator<Bid> comparator = new Comparator<Bid>() { 
     @Override 
     public int compare(Bid o1, Bid o2) { 
      if (o1.getBidprice() < o2.getBidprice()) 
       return -1; 
      if (o2.getBidprice() == o2.getBidprice()) 
       return 0; 
      return 1; 
     } 
    }; 
    Collections.sort(newLinkedList, comparator); 
    return newLinkedList; 
} 
+0

ありがとうございます。 oldbid.getprice()がnewLinkedList内の価格のいずれかと等しい場合は、newLinkedList BidオブジェクトをSortedリストに追加する必要がありますが、 oldLinkedListのoldBid one –

+0

はい、ソートされたリストとしてnewLinkedListを使用できます。 –

+0

ありがとうございます。それは私の問題を解決した –

0

ComparatorインターフェイスをBidクラスに実装し、Collections.sort()メソッドを使用できます。

関連する問題