简体   繁体   中英

Array getting overwritten with the last item java

I am having my array overwritten by the last Object and Am not sure why as a System.out.println() on the same Location prints all the items correctly. I am attempting to update a table containing Student scores with their ranks but all students get the last item on the array.

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();
        }
    }

I have seen similar questions but this looks strange as the System out prints them correctly. I'm I missing something here?

res.setSubjectRank(i + 1); is not this line updating the subject rank repeatedly in the loop? What do you expect this to do? In the first iteration it will update it and will keep updating till the last iteration and the last iteration will have the last item from the ranks array.

I finally found a way around this. I am going to post the Answer for the sake of anyone that might be experiencing the same problem. Since the Students having the same score will have the same rank, you can iterate for each score and Update the Rank. Like this:

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();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM