【问题标题】:Nested loop it sound start all over, from 1 and count up. But it dont嵌套循环听起来从头开始,从 1 开始计数。但它不
【发布时间】:2015-10-23 23:20:39
【问题描述】:

大家好,我正在尝试制作一个嵌套循环,第一个循环中的循环应该重新开始,而不是重复 161。正如您在图片中看到的那样,它失败了。而且我不知道如何解决它。

这是我的代码示例,我在失败的代码中留下了注释:

    int category_a = 1;
    String childname_a = "A";

    int category_b = 2;
    String childname_b = "B";

    int category_c = 3;
    String childname_c = "C";

    String description = "Indsæt beskrivelse her";


    try

    {

        db.beginTransaction();
        String sql = "INSERT INTO child (_idchild, category, childname, description) VALUES (?, ?, ?, ?)";
        SQLiteStatement statement = db.compileStatement(sql);



        for (int i = 1; i < 162; i++)
        {
                statement.clearBindings();
                statement.bindLong(1, i);
                statement.bindLong(2, category_a); //+ i
                statement.bindString(3, childname_a + i);
                statement.bindString(4, description);
                statement.executeInsert();

        }


        for (int j = 162; j < 325; j++)
        {
            statement.clearBindings();
            statement.bindLong(1, j);
            statement.bindLong(2, category_b); //+ i

             // It fails here, and do not start from 1 and count up to 162 as you can see.
            for (int h = 1; h < 162; h++)
            {
                statement.bindString(3, childname_b + h);
            }

            statement.bindString(4, description);
            statement.executeInsert();
        }





            db.setTransactionSuccessful(); // This commits the transaction if there were no exceptions

    }
    catch (Exception e)
    {
        Log.w("Exception:", e);
    } finally {
        db.endTransaction();
    }

the b section should start from 1 and go up, but et fails at repeating 161 as you can see.

【问题讨论】:

  • 这条线对我来说没有意义。 statement.bindString(3, childname_b + h);。它将覆盖每次迭代中的值,最终将其设置为 161。也许您可以告诉我们是否还有更多列要绑定这些变量。
  • 删除内部 for 循环并将其更改为 statement.bindString(3, childname_b + j);,而不是像第一个 for 循环中所做的那样。

标签: java loops nested


【解决方案1】:

如果您想要每个 j 的记录 B1 到 B162,则如下所示。可能在内部循环之前绑定描述并且只绑定字符串3并在循环中执行插入就足够了

for (int j = 162; j < 325; j++)
{
    for (int h = 1; h < 162; h++)
    {
        statement.clearBindings();
        statement.bindLong(1, j);
        statement.bindLong(2, category_b); //+ i
        statement.bindString(3, childname_b + h);
        statement.bindString(4, description);
        statement.executeInsert();
    }
}

【讨论】:

    【解决方案2】:

    您似乎在内部循环中缺少statement.executeInsert();

    【讨论】:

      【解决方案3】:

      改成

              for (int j = 162; j < 325; j++)
              {
                  statement.clearBindings();
                  statement.bindLong(1, j);
                  statement.bindLong(2, category_b); //+ i
                  statement.bindString(3, childname_b + j);
                  statement.bindString(4, description);
                  statement.executeInsert();
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-30
        • 2012-08-31
        • 2012-09-15
        • 1970-01-01
        相关资源
        最近更新 更多