在Android 2.3.3中迭代for循环

时间:2012-12-22 03:57:52

标签: android

Android 2.3.3 问题很简单。循环迭代12次,因为它应该迭代11次,就我的逻辑而言。有人能发现问题吗...我在俯瞰什么?

c = db.getData();

if(c.getCount() > 0)
{
    c.moveToFirst();
    System.out.println("Cursor Values are ::: "+c.toString());
    int columCount = c.getColumnCount();
    System.out.println("Column Count is ::: "+columCount);
    boolean isFound = false;


    do
    {
         for(int iteCount = 0; i < columCount; iteCount++)
        {
            System.out.println("Loop ::: "+iteCount);
            String tempString =  c.getString(iteCount);
            System.out.println("Item in TempString is :::"+tempString);

            if(tempString != null && tempString.equalsIgnoreCase(tempCommand))
            {
                    alFirstIndex.remove(0);
                System.out.println(c.getString(c.getColumnIndex("Command")));
               alFirstIndex.add(0, c.getString(c.getColumnIndex("Command")));
                isFound = true;
                break;
            }
        }
        System.out.println("Out of For Loop");

        if(isFound)
        {
           break;
        }

    }while(c.moveToNext());

这是logcat ::::

的输出
    12-22 09:13:08.219: I/System.out(25300): Column Count is ::: 11
12-22 09:13:08.219: I/System.out(25300): Loop ::: 0
12-22 09:13:08.219: I/System.out(25300): Item in TempString is :::ADD
12-22 09:13:08.219: I/System.out(25300): Loop ::: 1
12-22 09:13:08.219: I/System.out(25300): Item in TempString is :::add
12-22 09:13:08.219: I/System.out(25300): Loop ::: 2
12-22 09:13:08.219: I/System.out(25300): Item in TempString is :::and
12-22 09:13:08.219: I/System.out(25300): Loop ::: 3
12-22 09:13:08.219: I/System.out(25300): Item in TempString is :::had
12-22 09:13:08.219: I/System.out(25300): Loop ::: 4
12-22 09:13:08.219: I/System.out(25300): Item in TempString is :::ad
12-22 09:13:08.219: I/System.out(25300): Loop ::: 5
12-22 09:13:08.219: I/System.out(25300): Item in TempString is :::anddd
12-22 09:13:08.219: I/System.out(25300): Loop ::: 6
12-22 09:13:08.219: I/System.out(25300): Item in TempString is :::anndd
12-22 09:13:08.219: I/System.out(25300): Loop ::: 7
12-22 09:13:08.219: I/System.out(25300): Item in TempString is :::null
12-22 09:13:08.219: I/System.out(25300): Loop ::: 8
12-22 09:13:08.219: I/System.out(25300): Item in TempString is :::null
12-22 09:13:08.219: I/System.out(25300): Loop ::: 9
12-22 09:13:08.219: I/System.out(25300): Item in TempString is :::null
12-22 09:13:08.219: I/System.out(25300): Loop ::: 10
12-22 09:13:08.219: I/System.out(25300): Item in TempString is :::null
12-22 09:13:08.219: I/System.out(25300): Loop ::: 11
12-22 09:13:08.219: E/CursorWindow(25300): Bad request for field slot 0,11. numRows = 18, numColumns = 11

columnCount = 11,循环以i = 0开头并且应该迭代到i < columCount,这意味着它应该迭代11次,但为什么它再次进入FOR循环并打印Loop ::: 11

1 个答案:

答案 0 :(得分:6)

在循环中:

for(int iteCount = 0; i < columCount; iteCount++)

您需要使用iteCount而不是i

for(int iteCount = 0; iteCount < columCount; iteCount++)