looping through array to find next matching element

P

Psybar Phreak

hi all

i have an array of Process objects, each elements has an id (process.id),
and arrival time (process.at), processing time (process.pt) and a boolean
indicating whether the process has completed (process.done).

atm, i have a loop that i going through the elements and check whether each
elements has been done, i am now at a point in the loop, where it has
discovered, that the current process (currentProcess) is done (ie.
currentProcess.done == 1).

what i want to do, is have a nested loop (im not sure what sort i need here,
this is where hopefully, one of you guys come in...) to continue going
through the array, and find the next elements where "done != 1"

i hope this question is clear, and hope even more so, that someone can help

thanks

PP
 
E

Eric Sosman

Psybar said:
hi all

i have an array of Process objects, each elements has an id (process.id),
and arrival time (process.at), processing time (process.pt) and a boolean
indicating whether the process has completed (process.done).

atm, i have a loop that i going through the elements and check whether each
elements has been done, i am now at a point in the loop, where it has
discovered, that the current process (currentProcess) is done (ie.
currentProcess.done == 1).

what i want to do, is have a nested loop (im not sure what sort i need here,
this is where hopefully, one of you guys come in...) to continue going
through the array, and find the next elements where "done != 1"

i hope this question is clear, and hope even more so, that someone can help

This seems so simple that I suspect I've misunderstood
the question ... Still:

Process[] array = ...
int i, j;
for (i = 0; i < array.length; ++i) {
if (array.done == 1)
break;
}
if (i >= array.length)
throw new NoProcessIsDoneException();
System.out.println("Process #" + i + " id " + array.id
+ " is done");

for (j = i; ++j < array.length; ) {
if (array[j].done != 1)
break;
}
if (j >= array.length)
throw new NoLaterProcessIsUndoneException();
System.out.println("Process #" + j + " id " + array[j].id
+ " is not done");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top