为什么这不起作用?我在trxFifoArray列表上循环,并将其对象项传递给result List。我需要将一些金额分成2,所以我需要将这2个金额添加到结果列表中。金额在arrayList中[9.0000,-60000]。所以我循环金额列表对结果进行addFirst,项目被添加,但金额相同,即使列表有2个不同的金额。
LinkedList<InvtQaTracer> trxFifoArray = new LinkedList<InvtQaTracer>();
LinkedList<InvtQaTracer> result = new LinkedList<InvtQaTracer>();
InvtQaTracer trx = new InvtQaTracer();
int trxDocoRef = 0;
for (int j = list.size() - 1; j >= 0; j--) {
trx = list.get(j);
System.out.printf("%12.4f %4s%10d %12s%n", trx.getTrxQty(), trx.getDocType(), trx.getOrdNo(), trx.getLocNo());
List<BigDecimal> auxAmounts = new ArrayList<BigDecimal>();
if (trx.getDocType().compareTo("OV") == 0
|| trx.getDocType().compareTo("XV") == 0) {
//Do something ...
} else {
BigDecimal auxAmount = BigDecimal.ZERO;
Boolean needRemove = false;
for (InvtQaTracer tFifo : trxFifoArray) {
if (trx.getDocType().compareTo("IT") == 0) {
auxAmounts.add(tFifo.getTrxQty().negate());
}
}
if (needRemove) {
Iterator<InvtQaTracer> iterator = trxFifoArray.iterator();
int count = 0;
while (iterator.hasNext()) {
InvtQaTracer iqt = iterator.next();
if (iqt.getTrxQty().compareTo(BigDecimal.ZERO) == 0) {
count++;
iterator.remove();
}
}
}
}
if (!auxAmounts.isEmpty()) {
for (BigDecimal asss : auxAmounts) {
System.out.println(asss);
trx.setTrxQty(asss);
result.addFirst(trx);
}
} else {
result.addFirst(trx);
}
for (InvtQaTracer invtQT : trxFifoArray) {
System.out.printf("%20s%2s%12.4f %10d %12s%10d%n", " ----------------> ", invtQT.getDocType(), invtQT.getTrxQty(), invtQT.getOrdNo(), invtQT.getLocNo(), invtQT.getDocNo());
}
}
这段代码添加了两条值为-6.000000的记录,即使它正在打印这两条记录。我希望你能理解代码。请帮忙!!!
谢谢。
谢谢你的帮助!
Am在代码中发现问题。代码需要像这个一样更新
if (!auxAmounts.isEmpty()) {
LinkedList<InvtQaTracer> result = new LinkedList<InvtQaTracer>();
//Updated Here
InvtQaTracer trx = null;
List<BigDecimal> auxAmounts = new ArrayList<BigDecimal>(); //[-9.000000, -6.000000]
for (BigDecimal asss : auxAmounts) {
System.out.println(asss);
//Updated Here
trx = new InvtQaTracer();
trx.setTrxQty(asss);
result.addFirst(trx);
}
} else {
result.addFirst(trx);
}
确定的另一个变化是在else循环中needRemove值默认设置为false,下面检查if循环中的值是否为true。我觉得这个可以去掉。