我有交易的列表,每个行业现在有一个像源的一些属性,从行业的名单,我想要得到的是具有相同源值各行各业将它们组合成一个行业,例如如果条件在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