Last active
July 3, 2018 01:50
-
-
Save lucaspolo/6264482 to your computer and use it in GitHub Desktop.
Iterando em uma matriz de duas dimensões, em apenas uma iteração.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void matriz() { | |
int [][] vetor = new int[5][5]; | |
int tamanhoVetor = vetor.length; | |
int tamanhoSubVetor = vetor[1].length; | |
int tamanho = tamanhoVetor * tamanhoSubVetor; | |
for(int i = 0; i < tamanho; i++) { | |
int indiceDoSubvetor = i % tamanhoVetor; | |
int indiceDoVetor = (i - indiceDoSubvetor) / tamanhoSubVetor; | |
System.out.println("Vetor: " + indiceDoVetor + " Subvetor: " + indiceDoSubvetor); | |
vetor[indiceDoVetor][indiceDoSubvetor] = i; | |
} | |
for(int i = 0; i < tamanho; i++) { | |
int indiceDoSubvetor = i % tamanhoSubVetor; | |
int indiceDoVetor = (i - indiceDoSubvetor) / tamanhoVetor; | |
System.out.println(vetor[indiceDoVetor][indiceDoSubvetor]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment