我有交易的列表,每個行業現在有一個像源的一些屬性,從行業的名單,我想要得到的是具有相同源值各行各業將它們組合成一個行業,例如如果條件在arraylist中滿足,如何比較元素併合並?
tradeName quote source quantity price
Google GOOG Goldman Sachs 15 610
Microsoft MSFT Barclays 400 28
Google GOOG Goldman Sachs 45 610
Google GOOG Goldman Sachs 40 610
Microsoft MSFT Barclays 1000 28
現在基於源的信息,我應該結合行業,所以我更新的貿易清單將是
tradeName quote source quantity price
Google GOOG Goldman Sachs 100 610
Microsoft MSFT Barclays 1400 28
我不知道比較部分,如何去解決呢?
試圖
下面的方法,
for (Trade trade : tradeList)
{
//Not sure how to compare this.trade.source with all sources
//of all trades present in the trade.
//Logic should be if source matches then quantity should be added
//but am not sure how comparison would work.
}
Class Trade
{
private tradeName;
private quote;
private source;
private quantity;
private price;
//Getters and Setters for each of above mentioned attributes.
}
你有什麼試過?你需要知道如何編寫循環?循環內的比較?你究竟需要知道什麼?一個僞代碼算法? – 2012-08-02 15:59:43
@ngmiceli:我正在遍歷循環,但現在我將如何在循環內與arraylist的所有元素進行比較,這是我掛斷的地方。 – Rachel 2012-08-02 16:00:28
如果您向我們展示您目前爲迭代循環所寫的內容,我們可以更好地幫助 – 2012-08-02 16:01:02