Java while循环不限制作用域?
工作在一个侧面项目上,试图遍历一个flex表并拉取数据。该表中有一个包含另一个表的列,该表包含与其关联的行的行。我首先获取数据,然后获取与其关联的行,将其填充到程序集中,然后将其存储在数组列表中。我的问题是,它正确地提取数据,它不存储到数组列表中。它覆盖了所有以前的行,我无法弄清楚为什么。这是while循环" title="while循环">while循环的范围问题吗?我没有正确地宣布一些东西吗?代码如下Java while循环不限制作用域?
private static void submitOrder() { if (nullCheck()) {
ArrayList<AssemblyLine> assemblyLines = new ArrayList<AssemblyLine>();
HorizontalPanel hPanel = (HorizontalPanel) fTable.getWidget(fTable.getRowCount() - 1, 0);
CheckBox cb1 = (CheckBox) hPanel.getWidget(0);
int rowSpan = 0;
int currentRow = 1;
int currentAssemblyLineRow = 1;
while (currentRow < fTable.getRowCount() - 2) {
// get current info table and set initial data
FlexTable currentInfoTable = (FlexTable) fTable.getWidget(currentRow, 7);
rowSpan = fTable.getFlexCellFormatter().getRowSpan(currentRow, 7);
AssemblyLine al = new AssemblyLine();
ListBox assembler = (ListBox) currentInfoTable.getWidget(2, 1);
DateBox date = (DateBox) currentInfoTable.getWidget(3, 1);
al.setAssemblerName(assembler.getValue(assembler.getSelectedIndex()));
al.setDate(date.getValue());
al.setAssemblyID(Integer.parseInt(fTable.getHTML(currentRow, 0)));
// if "use all info for lots" is checked set assembler and date
// to that
//other data set here
// iterate through related rows and store assemblylines in
// database, I think the error is in here
while (currentAssemblyLineRow < (rowSpan + currentRow)) {
al.setQuantity(Integer.parseInt(fTable.getHTML(currentAssemblyLineRow, 5)));
System.out.println("al.getQuantity: " + al.getQuantity());
ListBox lb = (ListBox) fTable.getWidget(currentAssemblyLineRow, 6);
al.setPartLotNumber(lb.getItemText(lb.getSelectedIndex()));
System.out.println("al.getPartLotNumber: " + al.getPartLotNumber());
assemblyLines.add(al);
System.out.println("al.tostring: " + al.toString());
System.out.println("a.tostring: " + assemblyLines.get(assemblyLines.size() - 1).toString());
currentAssemblyLineRow++;
}
for (int k = 0; k < assemblyLines.size(); k++) {
System.out.println("a.tostring: " + k + " " + assemblyLines.get(k).toString());
}
// then iterate to next infotable
currentRow = currentRow + rowSpan;
}
DatabaseUtils.submitAssemblyLines(assemblyLines);
} else {
Window.alert("Oops! please fill out all boxes!");
}
}
这里是从控制台
al.getQuantity: 1 al.getPartLotNumber: 43528
al.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=43528, quantity=1]
a.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=43528, quantity=1]
al.getQuantity: 1
al.getPartLotNumber: H17253
al.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=H17253, quantity=1]
a.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=H17253, quantity=1]
al.getQuantity: 1
al.getPartLotNumber: 31561414
al.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=31561414, quantity=1]
a.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=31561414, quantity=1]
al.getQuantity: 1
al.getPartLotNumber: 001122
al.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=001122, quantity=1]
a.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=001122, quantity=1]
al.getQuantity: 2
al.getPartLotNumber: 123456
al.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=123456, quantity=2]
a.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=123456, quantity=2]
al.getQuantity: 1
al.getPartLotNumber: 103534
al.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=103534, quantity=1]
a.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=103534, quantity=1]
al.getQuantity: 1
al.getPartLotNumber: P12345
al.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 0 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 1 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 2 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 3 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 4 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 5 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 6 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
al.getQuantity: 1
al.getPartLotNumber: H17253
al.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=13, AssemblerID=0, AssemblerName=Blake, date=Fri Aug 02 12:00:00 PDT 2013, partLotNumber=H17253, quantity=1]
a.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=13, AssemblerID=0, AssemblerName=Blake, date=Fri Aug 02 12:00:00 PDT 2013, partLotNumber=H17253, quantity=1]
al.getQuantity: 2
al.getPartLotNumber: 001122
al.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=13, AssemblerID=0, AssemblerName=Blake, date=Fri Aug 02 12:00:00 PDT 2013, partLotNumber=001122, quantity=2]
a.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=13, AssemblerID=0, AssemblerName=Blake, date=Fri Aug 02 12:00:00 PDT 2013, partLotNumber=001122, quantity=2]
al.getQuantity: 1
al.getPartLotNumber: P12345
al.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=13, AssemblerID=0, AssemblerName=Blake, date=Fri Aug 02 12:00:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=13, AssemblerID=0, AssemblerName=Blake, date=Fri Aug 02 12:00:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 0 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 1 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 2 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 3 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 4 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 5 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 6 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=12, AssemblerID=0, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 7 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=13, AssemblerID=0, AssemblerName=Blake, date=Fri Aug 02 12:00:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 8 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=13, AssemblerID=0, AssemblerName=Blake, date=Fri Aug 02 12:00:00 PDT 2013, partLotNumber=P12345, quantity=1]
a.tostring: 9 AssemblyLine [AssemblyLineID=0, AssemblyLotNumber=0, AssemblyID=13, AssemblerID=0, AssemblerName=Blake, date=Fri Aug 02 12:00:00 PDT 2013, partLotNumber=P12345, quantity=1]
AssemblyLot at 0: Assembly [assemblyLotNumber=1, assemblyID=12, name=null, partID=null, quantities=null]
AssemblyLot at 1: Assembly [assemblyLotNumber=2, assemblyID=13, name=null, partID=null, quantities=null]
AssemblyLines at 0: AssemblyLine [AssemblyLineID=7, AssemblyLotNumber=1, AssemblyID=12, AssemblerID=1, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
AssemblyLines at 1: AssemblyLine [AssemblyLineID=7, AssemblyLotNumber=1, AssemblyID=12, AssemblerID=1, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
AssemblyLines at 2: AssemblyLine [AssemblyLineID=7, AssemblyLotNumber=1, AssemblyID=12, AssemblerID=1, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
AssemblyLines at 3: AssemblyLine [AssemblyLineID=7, AssemblyLotNumber=1, AssemblyID=12, AssemblerID=1, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
AssemblyLines at 4: AssemblyLine [AssemblyLineID=7, AssemblyLotNumber=1, AssemblyID=12, AssemblerID=1, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
AssemblyLines at 5: AssemblyLine [AssemblyLineID=7, AssemblyLotNumber=1, AssemblyID=12, AssemblerID=1, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
AssemblyLines at 6: AssemblyLine [AssemblyLineID=7, AssemblyLotNumber=1, AssemblyID=12, AssemblerID=1, AssemblerName=John, date=Thu Aug 01 14:54:00 PDT 2013, partLotNumber=P12345, quantity=1]
AssemblyLines at 7: AssemblyLine [AssemblyLineID=10, AssemblyLotNumber=2, AssemblyID=13, AssemblerID=2, AssemblerName=Blake, date=Fri Aug 02 12:00:00 PDT 2013, partLotNumber=P12345, quantity=1]
AssemblyLines at 8: AssemblyLine [AssemblyLineID=10, AssemblyLotNumber=2, AssemblyID=13, AssemblerID=2, AssemblerName=Blake, date=Fri Aug 02 12:00:00 PDT 2013, partLotNumber=P12345, quantity=1]
AssemblyLines at 9: AssemblyLine [AssemblyLineID=10, AssemblyLotNumber=2, AssemblyID=13, AssemblerID=2, AssemblerName=Blake, date=Fri Aug 02 12:00:00 PDT 2013, partLotNumber=P12345, quantity=1]
回答:
用户需要通过环新AssemblyLine
每次分配输出。现在,你所做的只是一遍又一遍地添加同一个对象(每次通过循环修改它)。
ListBox assembler = (ListBox) currentInfoTable.getWidget(2, 1); DateBox date = (DateBox) currentInfoTable.getWidget(3, 1);
while (currentAssemblyLineRow < (rowSpan + currentRow)) {
AssemblyLine al = new AssemblyLine();
al.setAssemblerName(assembler.getValue(assembler.getSelectedIndex()));
al.setDate(date.getValue());
al.setAssemblyID(Integer.parseInt(fTable.getHTML(currentRow, 0)));
// rest of loop body goes here (current code)
}
以上是 Java while循环不限制作用域? 的全部内容, 来源链接: utcz.com/qa/259597.html