数组被最后一项java覆盖

时间:2017-09-09 04:04:25

标签: java arrays

我的数组被最后一个Object覆盖,我不确定为什么同一位置上的System.out.println()会正确打印所有项目。我正在尝试更新包含学生成绩的表格,但所有学生都会获得阵列中的最后一项。

public static void updateSubjectRank(String subject, String extype, int academicyear, int semester, int level) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Criteria cr = session.createCriteria(EduResults.class);
            cr.add(Restrictions.eq("subject", subject));
            cr.add(Restrictions.eq("extype", extype));
            cr.add(Restrictions.eq("acedemicyear", academicyear));
            cr.add(Restrictions.eq("semester", semester));
            cr.add(Restrictions.eq("level", level));
            ScrollableResults items = cr.scroll();
            int count = 0;
            double[] scores = getSubjectScores(subject, extype, academicyear, semester, level);//this gets all the scores in a double array
            int[] ranks = Rank.getRank(scores);//this gives me the ranks of the scores above
            while (items.next()) {
                EduResults res = (EduResults) items.get(0);
                for (int i : ranks) {
                    res.setSubjectRank(i + 1);//this updates the database with the last item in the array
                    System.out.println(i+1);///this prints the ranks properly
                    session.saveOrUpdate(res);
                }
                if (++count % 100 == 0) {
                    session.flush();
                    session.clear();
                }
            }
            tx.commit();
        } catch (Exception asd) {
            log.debug(asd.getMessage());
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            session.close();
        }
    }

我见过类似的问题,但这看起来很奇怪,因为系统输出正确。我在这里错过了什么?

2 个答案:

答案 0 :(得分:0)

res.setSubjectRank(i + 1);不是这一行在循环中反复更新主题排名吗?你期望这样做什么?在第一次迭代中,它将更新它并将继续更新直到最后一次迭代,最后一次迭代将具有来自rank数组的最后一项。

答案 1 :(得分:0)

我终于找到了解决这个问题的方法。我将为可能遇到同样问题的任何人发布答案。由于具有相同分数的学生将具有相同的等级,您可以迭代每个分数并更新等级。像这样:

public static void updateSubjectRank(String subject, String extype, int academicyear, int semester, int level) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            double[] scores = getSubjectScores(subject, extype, academicyear, semester, level);
            int[] ranks = Rank.getRank(scores);
            for (double d : scores) {
                Criteria cr = session.createCriteria(EduResults.class);
                int rank = Rank.getRank(Rank.getRank(scores), Rank.getArrayIndex(scores, d)) + 1;
                cr.add(Restrictions.eq("subject", subject));
                cr.add(Restrictions.eq("extype", extype));
                cr.add(Restrictions.eq("acedemicyear", academicyear));
                cr.add(Restrictions.eq("semester", semester));
                cr.add(Restrictions.eq("level", level));
                cr.add(Restrictions.eq("scorePcnt", d));
                ScrollableResults items = cr.scroll();
                int count = 0;
                while (items.next()) {
                    EduResults res = (EduResults) items.get(0);
                    res.setSubjectRank(rank);
                    System.out.println(rank);
                    session.saveOrUpdate(res);
                    if (++count % 100 == 0) {
                        session.flush();
                        session.clear();
                    }
                }
            }

            tx.commit();
        } catch (Exception asd) {
            log.debug(asd.getMessage());
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            session.close();
        }
    }