开发者社区> 问答> 正文

循环内定义变量和循环外定义变量的问题

循环外定义变量 tempReportInfo

public ArrayList<ReportInfo> getAllReportInfos() {
        ArrayList<ReportInfo> reportInfos = new ArrayList<ReportInfo>();
        Cursor cursor = null;
        ReportInfo tempReportInfo = new ReportInfo();

        synchronized (helper) {
            if (!db.isOpen()) {
                db = helper.getWritableDatabase();
            }
            cursor = db.rawQuery("SELECT * FROM " + DBKeysName.TABLE_REPORT,
                    null);
            try {
                if (cursor != null) {
                    while (cursor.moveToNext()) {
                        tempReportInfo.setUserIds(cursor.getString(cursor
                                .getColumnIndex(DBKeysName.ROW_REPORT_USERID)));
                        tempReportInfo
                                .setStationName(cursor.getString(cursor
                                        .getColumnIndex(DBKeysName.ROW_REPORT_STATIONNAME)));
                        tempReportInfo.setTime(cursor.getString(cursor
                                .getColumnIndex(DBKeysName.ROW_REPORT_TIME)));
                        tempReportInfo.setLineId(cursor.getInt(cursor
                                .getColumnIndex(DBKeysName.ROW_REPORT_LINEID)));
                        tempReportInfo.setType(cursor.getInt(cursor
                                .getColumnIndex(DBKeysName.ROW_REPORT_TYPE)));
                        tempReportInfo.setRole(cursor.getInt(cursor
                                .getColumnIndex(DBKeysName.ROW_REPORT_ROLE)));
                        tempReportInfo.setFlag(cursor.getInt(cursor
                                .getColumnIndex(DBKeysName.ROW_REPORT_FLAG)));
                        reportInfos.add(tempReportInfo);
                    }
            }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            } finally {
                cursor.close();
            }

        }
        return reportInfos;

    }

上述例子得到的结果是,reportInfos添加的是数量为与数据表里行数一样的,但内容为数据表里第一条数据.
循环内定义变量tempReportInfo

public ArrayList<ReportInfo> getAllReportInfos() {
        ArrayList<ReportInfo> reportInfos = new ArrayList<ReportInfo>();
        Cursor cursor = null;
        synchronized (helper) {
            if (!db.isOpen()) {
                db = helper.getWritableDatabase();
            }
            cursor = db.rawQuery("SELECT * FROM " + DBKeysName.TABLE_REPORT,
                    null);

            try {
                if (cursor != null) {
                    while (cursor.moveToNext()) {
                        ReportInfo tempReportInfo = new ReportInfo();
                        tempReportInfo.setUserIds(cursor.getString(cursor
                                .getColumnIndex(DBKeysName.ROW_REPORT_USERID)));
                        tempReportInfo
                                .setStationName(cursor.getString(cursor
                                        .getColumnIndex(DBKeysName.ROW_REPORT_STATIONNAME)));
                        tempReportInfo.setTime(cursor.getString(cursor
                                .getColumnIndex(DBKeysName.ROW_REPORT_TIME)));
                        tempReportInfo.setLineId(cursor.getInt(cursor
                                .getColumnIndex(DBKeysName.ROW_REPORT_LINEID)));
                        tempReportInfo.setType(cursor.getInt(cursor
                                .getColumnIndex(DBKeysName.ROW_REPORT_TYPE)));
                        tempReportInfo.setRole(cursor.getInt(cursor
                                .getColumnIndex(DBKeysName.ROW_REPORT_ROLE)));
                        tempReportInfo.setFlag(cursor.getInt(cursor
                                .getColumnIndex(DBKeysName.ROW_REPORT_FLAG)));
                        reportInfos.add(tempReportInfo);
                    }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
        } finally {
                cursor.close();
            }

        }
        return reportInfos;

    }

这样才能得到想要的结果:reportInfos添加了数据表里的所有数据.请问一下,为什么循环外定义变tempReportInfo就得到不一样的结果呢?

展开
收起
蛮大人123 2016-03-18 11:49:56 2053 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    放外面也行;但是需要每次循环都加上
    tempReportInfo = new ReportInfo();否则只创建了一个对象,你修改来修改去,添加到集合的都是指向同一个对象的引用

    2019-07-17 19:06:19
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
重新定义计算的边界 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载