Doubt regarding the for function

1
for(int i=0; i<mat.length; i++){
            for(int j=0; j<matrizFemininaArryn.length; j++){
             if(mat[i][5].equals("Feminino") && mat[i][4].equals("Arryn")) matrizFemininaArryn[j] = mat[i][0];
            }    
}

In the above example, will matrizFemininaArryn receive matriz[i][0] until it fills its correct full size? However my goal was that when executing the for to the first position of the matrizFemininaArryn vector, execute the for of the i again, so as not to apply the same result to the next position of the matrizFemininaArryn p>

How to proceed?

    
asked by anonymous 17.05.2015 / 22:26

1 answer

1

There are two commands that can be used to modify the normal looping run: break and continue

The break causes the loop to terminate immediately, continuing execution from the statement following the key that closes the loop br> The code between break and key is ignored.

This example code looks for the number 12 in array . The break ends the loop when the value is found.

class BreakDemo {
    public static void main(String[] args) {

        int[] arrayOfInts = 
            { 32, 87, 3, 589,
              12, 1076, 2000,
              8, 622, 127 };
        int searchfor = 12;

        int i;
        boolean foundIt = false;

        for (i = 0; i < arrayOfInts.length; i++) {
            if (arrayOfInts[i] == searchfor) {
                foundIt = true;
                break;
            }
        }

        if (foundIt) {
            System.out.println("Found " + searchfor + " at index " + i);
        } else {
            System.out.println(searchfor + " not in the array");
        }
    }
}

The continue causes the execution to jump to the line where the key that closes the loop code exists, causing the condition to be evaluated, loop will run again if it is true .
The code between continue and key is ignored.

The code in the following example runs through string counting all occurrences of the letter 'p'. If the parsed letter is not a 'p' then continue causes the next code to be ignored and move to the next letter.

class ContinueDemo {
    public static void main(String[] args) {

        String searchMe = "peter piper picked a " + "peck of pickled peppers";
        int max = searchMe.length();
        int numPs = 0;

        for (int i = 0; i < max; i++) {

            if (searchMe.charAt(i) != 'p')
                continue; // se não é 'p' buscar próxima letra

            // Mais um 'p' encontrado
            numPs++;
        }
        System.out.println("Found " + numPs + " p's in the string.");
    }
} 

If I understood your question correctly, what you are looking for is break . Your code would look like this:

for(int i=0; i<mat.length; i++){
    for(int j=0; j<matrizFemininaArryn.length; j++){
        if(mat[i][5].equals("Feminino") && mat[i][4].equals("Arryn")){
            matrizFemininaArryn[j] = mat[i][0];
            break;
        }
     }
}//após a execução do break a execução salta para aqui terminado o ciclo 'j'
 //continuando o ciclo 'i'

Both break and continue act in relation to loop where they are inserted.
If you want them to refer to the outermost loops, you associate them with a label that indicates what loop refer to.

Suppose you wanted your code to end both loops when the if condition was true. The code would look like this:

//Identifica-se o loop com um 'label'
search:
for(int i=0; i<mat.length; i++){
    for(int j=0; j<matrizFemininaArryn.length; j++){
        if(mat[i][5].equals("Feminino") && mat[i][4].equals("Arryn")){
            matrizFemininaArryn[j] = mat[i][0];
            break search; //Adicionado o 'label' seach
        }
     }
}
//após a execução do break a execução salta para aqui terminado o ciclo 'j'e o ciclo 'i'

Sample Code Source: Java Documentation

    
17.05.2015 / 23:16