Java 8 : How to update data in a linked list, where I need to pull the data of previous node to update data of current node

huangapple go评论80阅读模式
英文:

Java 8 : How to update data in a linked list, where I need to pull the data of previous node to update data of current node

问题

以下是您提供的内容的翻译部分:

我目前在处理Java的双向链表问题。我需要根据前一个节点的值来更新当前节点的数据。如果我尝试这样做,它会陷入无限循环:

ListIterator<SegmentDTO> taskListIterator = segmentDTOLinkedList.listIterator(dataPosition);
while (taskListIterator.hasNext())
{
    SegmentDTO existingSegment = taskListIterator.next();
    existingSegment.setSequenceId(existingSegment.getSequenceId() + 1);

    if (taskListIterator.hasPrevious())
    {
        SegmentDTO previousSegment = taskListIterator.previous();
        LocalDateTime projectedStartDttm = existingSegment.getProjectedStartDttm();
        LocalDateTime projectedEndDttm = existingSegment.getProjectedEndDttm();

        long originalDifference = Duration.between(projectedStartDttm, projectedEndDttm).toHours();
        LocalDateTime projectedEndDateTimeAfterNewStop = previousSegment.getProjectedEndDttm()
                .plusHours(originalDifference);

        existingSegment.setProjectedStartDttm(previousSegment.getProjectedEndDttm());
        existingSegment.setProjectedEndDttm(projectedEndDateTimeAfterNewStop);
    }
}

我的hasNext()函数无法跳出while循环。每当我尝试访问先前的数据时,它的值就会重置为之前的值。

只有在我去掉对previous()的调用时,循环才会结束,但我需要数据来根据前一个数据更新当前记录。请帮忙解决。

英文:

I am currently stuck in java doubly linked list. I need to update the data of current node, based on the value of previous. It's going into a never ending loop if I try this :

 ListIterator&lt;SegmentDTO&gt; taskListIterator = segmentDTOLinkedList.listIterator(dataPosition);
            while (taskListIterator.hasNext())
            {
                SegmentDTO existingSegment = taskListIterator.next();
                existingSegment.setSequenceId(existingSegment.getSequenceId() + 1);

                if (taskListIterator.hasPrevious())
                {
                    SegmentDTO previousSegment = taskListIterator.previous();
                    LocalDateTime projectedStartDttm = existingSegment.getProjectedStartDttm();
                    LocalDateTime projectedEndDttm = existingSegment.getProjectedEndDttm();

                    long originalDifference = Duration.between(projectedStartDttm, projectedEndDttm).toHours();
                    LocalDateTime projectedEndDateTimeAfterNewStop = previousSegment.getProjectedEndDttm()
                            .plusHours(originalDifference);

                    existingSegment.setProjectedStartDttm(previousSegment.getProjectedEndDttm());
                    existingSegment.setProjectedEndDttm(projectedEndDateTimeAfterNewStop);
                }
            }

my hasNext() function is not coming out of while loop. It value is getting reset to previous, whenever I am trying to access previous data.

While loop end only if, I remove the call to previous(), but I need the data to update the current record based on previous. Please help.

答案1

得分: 2

代码在每次执行 while 循环时都会通过 ListIterator 前进后退。在你调用 taskListIterator.previous() 后,在 if 语句中调用 taskListIterator.next() 使其前进。

英文:

The code is moving forward then back again through the ListIterator every time the while loop is executed. Call taskListIterator.next() in your if statement after you call taskListIterator.previous() to move it back forward.

huangapple
  • 本文由 发表于 2020年5月4日 04:57:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/61581513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定