Java while循环不限制范围?

时间:2013-08-01 22:11:23

标签: java gwt loops while-loop

处理辅助项目,尝试遍历flex表并提取数据。该表有一列,其中包含另一个表,该表跨越与其关联的行的行。我先获取该数据,然后获取与之关联的行,将其填充到一个程序集中,然后将其存储在一个arraylist中。我的问题是它正确地提取数据,而不是存储到arraylist中。它覆盖了所有以前的行,我无法弄清楚原因。这是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]

1 个答案:

答案 0 :(得分: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)
}