虽然循环不会停止循环Java

时间:2013-04-26 09:57:43

标签: java loops while-loop iteration

我创建了名为Reservations的简单表,此表包含以下列:ID,DUE_DATE,PRODUCTS_PAID,RESERVATION_NAME,SALE,TOTAL_SELL_PRICE和CUSTOMER_ID。

现在我想做的是我想要迭代这个CUSTOMER_ID给定值的表。这是我的方法,它通过CUSTOMER_ID收集所有这些列数据:

   public Collection<Reservations> findReservation(int cuID) {
    EntityManager em = getEntityManager();
    Query q = em.createQuery("SELECT r FROM Reservations r WHERE r.customerData.id = :cID");
    q.setParameter("cID", cuID);
    List results = q.getResultList();
    return results;
}

我的问题在于,每当我运行迭代循环时,它都不会停止迭代,但会继续迭代。我在该表中只有两行数据。你能告诉我为什么这个迭代会继续并且不会停止吗?显然,我只想基于给定的CUSTOMER_ID值迭代该表内的所有数据。

我已尝试过for循环,对于每个循环和while循环而非循环工作。它必须与我的代码有关。这是我测试的一个while循环,但它不会像其他循环一样停止迭代:

       for(Iterator itr2 = rd.findReservation(cuID).iterator(); itr2.hasNext();){
        rModel.addRow(Arrays.asList(rd.findReservation(cuID).iterator().next().getId(), 
                rd.findReservation(cuID).iterator().next().getReservationName(),
                rd.findReservation(cuID).iterator().next().getCustomerData().getCustomerName(),
                rd.findReservation(cuID).iterator().next().getCustomerData().getCustomerType(),
                rd.findReservation(cuID).iterator().next().getDueDate(),
                rd.findReservation(cuID).iterator().next().getTotalSellPrice(),
                rd.findReservation(cuID).iterator().next().getSale(),
                rd.findReservation(cuID).iterator().next().getProductsPaid(),
                "Print"));
    }

如果你想知道什么是cuID,它只是一个从表列接收的整数值。该值为customerID。它运作良好。所以它不应该对代码产生影响。所有帮助表示赞赏:)

3 个答案:

答案 0 :(得分:2)

因为您没有调用您在循环中声明的next上的iterator。试试这个:

for(Iterator itr2 = rd.findReservation(cuID).iterator(); itr2.hasNext();){
    MyObject obj = itr2.next();
    rModel.addRow(Arrays.asList(obj.getId(), 
                                obj.getReservationName(),
                                obj.getCustomerData().getCustomerName(),
                                obj.getCustomerData().getCustomerType(),
                                obj.getDueDate(),
                                obj.getTotalSellPrice(),
                                obj.getSale(),
                                obj.getProductsPaid(),
                                "Print"));
}

答案 1 :(得分:1)

你需要做这样的事情: -

for(Reservations res : rd.findReservation(cuID)){
    // Do whatever you want with this object(res)
    rModel.addRow(Arrays.asList(res.getId(), res.getReservationName()...);
}

这很干净简单!不需要以你曾经使用的奇怪方式使用迭代器,它只是不断获取一个新的迭代器并继续从中获取第一个条目,从而导致infinite循环!

答案 2 :(得分:1)

你使用两个独立的迭代器。你做的是这个:

while (iterator1.hasNext()) {
  iterator2.next();
}
显然你不会耗尽iterator1。在循环和rModel行中使用相同的迭代器:

   Iterator it = rd.findReservation(cuID).iterator();
   while (it.hasNext()) {
     // or whatever your type is
     Object next = it.next();

    rModel.addRow(Arrays.asList(next.getId(), 
            next.getReservationName(),
            next.getCustomerData().getCustomerName(),
            next.getCustomerData().getCustomerType(),
            next.getDueDate(),
            next.getTotalSellPrice(),
            next.getSale(),
            next.getProductsPaid(),
            "Print"));
}
相关问题