对于循环似乎运行两次

时间:2017-09-06 01:02:15

标签: java sql for-loop

编辑:如果我问这个错,我很抱歉。我的预期结果将是这个for循环运行两次总计。我只期望一个: 的System.out.println(库存[0] [1]); 的System.out.println(库存[1] [1]);

我认为这是一个简单的for循环从数据库中提取,并将每个结果添加到String数组中。

while(rs3.next()) {
    for(int c = 0; c < iCount; c++) {
        inventory[c][0] = rs3.getString(10);                    
        inventory[c][1] = rs3.getString(9);                     

        System.out.println(inventory[c][1]); //#&(*& WHY IS THIS HAPPENING TWICE

        inventory[c][3] = Integer.toString(rs3.getInt(3));      
        inventory[c][6] = rs3.getString(6);                     
        inventory[c][10] = Integer.toString(rs3.getInt(12));    
        inventory[c][12] = Integer.toString(rs3.getInt(13));    
        inventory[c][31] = Integer.toString(rs3.getInt(14));    
        inventory[c][32] = Integer.toString(rs3.getInt(15));    
        inventory[c][34] = Integer.toString(rs3.getInt(16));    
        inventory[c][52] = rs3.getString(4);                    
        inventory[c][53] = Integer.toString(rs3.getInt(5)); 
        inventory[c][18] = Integer.toString(rs3.getInt(7));     
        inventory[c][29] = Integer.toString(rs3.getInt(8));     
        inventory[c][78] = Integer.toString(rs3.getInt(1));     
    }
}

在此示例中,iCount设置为2,我使用System.out.println()检查。

问题是,它记录每个值两次。(每个循环,所以在这个例子中共计4次)这会抛弃使用这些数据的其他方法。

我添加了println(inventory [c] [1]);验证它确实在for循环的PER LOOP中执行了两次这个动作。

1 个答案:

答案 0 :(得分:0)

我希望你想要:

int c = 0;
while(rs3.next()) {
    inventory[c][0] = rs3.getString(10);                    
    inventory[c][1] = rs3.getString(9);                     

    System.out.println(inventory[c][1]);

    // ... rest omitted for brevity

    c++;
}