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

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

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

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

放外面是每次循环都使用的那一个对象,放循环里面是每次循环都创建一个对象。你也可以在循环外面申请一个引用,然后在循环里面每次在new出来。

循环外定义会在堆里创建一个对象,然后再循环里的操作都反映在了这个对象上;循环里定义,每执行一次创建一个对象,然后操作的就是这个对象;