java软件开发面试题
发布网友
发布时间:2022-05-02 05:22
我来回答
共1个回答
热心网友
时间:2022-06-28 14:34
List<Tray> list2 = new ArrayList<Tray>();
for (Tray tray : list) {
// 合并自身商品 id 相同的托盘上的商品
List<Goods> gList0 = new ArrayList<Goods>();
for (Goods good : tray.goods) {
boolean flag = false;
for (Goods g0 : gList0) {
if (g0.id == good.id) {
g0.count += good.count;
flag = true;
}
}
if (!flag) {
gList0.add(good);
}
}
tray.goods = gList0;
// 合并托盘id 相同 的的商品
Tray t0 = null;
for (Tray tray2 : list2) {
if (tray2.id == tray.id) {
t0 = tray2;
break;
}
}
if (t0 == null) {
list2.add(tray);
} else {
for (Goods good : tray.goods) {
boolean flag = false;
for (Goods g0 : t0.goods) {
if (g0.id == good.id) {
g0.count += good.count;
flag = true;
}
}
if (!flag) {
t0.goods.add(good);
}
flag = false;
}
}
t0 = null;
}
list = list2;