• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

For cycle in Java

Aeren

n00b
Joined
Aug 29, 2005
Messages
37
Hi everyone!
This might sound a noob question, but i never used a for cycle this way before.
I have a Vector which contains self defined Classes. These classes have 3 variables, i want to choose the Element which has the lovest sourceIndex. Then i would like to remove it from the Vector and start it again. I thought of this:
Code:
Vector realMatches; //it's already filled
[ ... ]
    for( int j = 0; j < realMatches.size( ); j++ ) {
      int biggestElementIndex = 0;
      int biggestElementSIndex = realMatches.elementAt( 0 ).getSourceIndex( );
      for( int i = 1; i < realMatches.size( ); i++ ) {
        if ( realMatches.elementAt( i ).getSourceIndex( ) < biggestElementSIndex ) {
          biggestElementSIndex = realMatches.elementAt( i ).getSourceIndex( );
          biggestElementIndex  = i;
        }
      }
      realMatches.removeElementAt( biggestElementIndex );
    }
Question: If i have a vector of 3 elements, the inner cycle runs and removes one element, what happens then to the outer cycle? will realMatches.size( ) be calculated again(and set to 2) or will it be 3?
Another question: not using it here, but how can i use "break", to get out from the inner cycle, but still stay in the outer?

Thanks!

Aeren

PS: I can't test the code right now, but still i would like to know the working mechanics of my questions.
 
The realMatches.size() method i called every time the "j < realMatches.size( )" check is carried out. So you will always get the updated value.

I&#8217;m not sure about "break", I try to avoid it. Excessive use of "break" is generally a bad idea. If I remember correctly "break" should always exit from the current loop, method etc. Using "break" in the inner loop should therefore bring you to the outer loop. Try and see what happens, that&#8217;s usually the best way to learn new things.
 
nms said:
The realMatches.size() method i called every time the "j < realMatches.size( )" check is carried out. So you will always get the updated value.

I’m not sure about "break", I try to avoid it. Excessive use of "break" is generally a bad idea. If I remember correctly "break" should always exit from the current loop, method etc. Using "break" in the inner loop should therefore bring you to the outer loop. Try and see what happens, that’s usually the best way to learn new things.
TBH, i didn't even know if it was possible to define where the break shall lead us. I saw it on a java lesson, but i don't remember. I know you can define a label and use that for breaking, but i don't know what a simple break does. Oh well, let's try...
 
i vaguely remember reading something about using labels and breaking to that, but you can also do
break 2;
which will break out of 2 levels worth of loops. so if you do break 2; in the inner for loop, it will break out of both loops
 
Back
Top