java loop question
I need help with my looping. My target is that after the current ‘row’ is used, i’ll go back from the top and use another ‘row’
https://stackoverflow.com/q/79579567/10353761
Stack Overflow
Unanticipated Infinite Loop in Java
I'm currently having problem with an existing method. My target is after a patch has been added in this portion
while (!patchFile.getOLineList().get(j).code().startsWith("*TobeJclEnd")) {...
18 Replies
⌛
This post has been reserved for your question.
Hey @susmaria! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
My issue is after satisfying the while logic highlighted, it goes back to the first loop but the ‘row’ value doesnt change. Plus its causing now an infinite loop
What does
NativeFiles.tblJCLPatch()
return?
And is it possible that jclf.setOLineList(newpatchList);
interacts with it/changes the value returned by it?List of rows contains text like ‘STEP’, ‘S#’.
What
List
implementation?Its the last part of the method. After processing all lines from jclf.getOLineList. It will then proceed to set the new list.
So far it hasnt affected the logic yet. Its just after all the breaks, it doesnt continue to the next value of NativeFiles.tblJCLPatch()
ArrayList
? One of the immutable lists in java.util
?
move it down
right before the return;
You are setting a new list in the loopList<STRING>
What is the dynamic type?
Currently, you setting
newpatchList
to always bigger List
s in the loop
and then you are accessing the modified versions which get longer and longer
so you are adding stuff and then processing the stuff you just added resulting in more to processOne more thing is that after processing this snippet
while (!patchFile.getOLineList().get(j).code().startsWith("*TobeJclEnd")) {
JCLCode c = (JCLCode) patchFile.getOLineList().get(j);
newpatchList.add(c);
j++;
}
break;
I would like to return from the top and use the next value of the ‘row’ from NativeFiles.tblJCLPatch.
this is why i said earlier the setOLineList happens to not been affected because it wasnt accessed yet bc of the infinite looping
I’ve identified the cause of error is it that if there are two entries from tblJCLPatch needed for the process, only the first one is being processed by the whole method. After satisfying the while snippet, it does not proceed on processing the next entry and stuck at the first entry making the process infinite loop.
that
break;
doesn't fix the issue I mentionedMay i know which break; should i remove/move?
Why would you remove one now?
I am talking about the
jclf.setOLineList(newpatchList);
Does the method complete if you comment out that line?
or do you want to use a different newpatchList
in each iteration?
The problem is
because the newpatchList.add
increases jclf.getOLineList().size()
(starting in the second iteration of the outer loop)
maybe you want to make a copy but aren't
but I'm not sure what exactly you are intending to do thereAfter adding new values at newpatchList() the iteration continues until a) all entries from tblJCLPatch that satisfy the condition are processed b) all lines on the jclF.getOLineList are checked
After that the newpatchList is set. There would only be one newpatchList its just the values are appended
That's what you think
and it's correct for the first outer iteration
I’ve encountered this because instead of processing the next entry from tblJCLPatch, im stuck in one entry looping all over again
once you call
jclf.setOLineList(newpatchList);
, this is no longer true
because both jclf.getOLineList()
and newpatchList
are the same list
these aren't two lists with the same content, these are the same list
so once you call newpatchList.add
, you are instantly adding to jclf.getOLineList()
not just when you call jclf.setOLineList(newpatchList);
You want to instead create a copy
try changing jclf.setOLineList(newpatchList);
to jclf.setOLineList(new ArrayList<>(newpatchList));
or even jclf.setOLineList(List.copyOf(newpatchList));
if you don't need to modify the internal list
(you could also do that within setOLineList
)💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.