Mr_qyw 2015-08-20 07:18 采纳率: 100%
浏览 2236
已采纳

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

循环外定义变量 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就得到不一样的结果呢?

  • 写回答

3条回答 默认 最新

  • threenewbee 2015-08-20 07:28
    关注

    放外面也行
    但是需要每次循环都加上
    tempReportInfo = new ReportInfo();

    否则只创建了一个对象,你修改来修改去,添加到集合的都是指向同一个对象的引用

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制