Skip to content

Instantly share code, notes, and snippets.

@xlbruce
Created September 19, 2015 19:47
Show Gist options
  • Save xlbruce/c387524c3621c5a72275 to your computer and use it in GitHub Desktop.
Save xlbruce/c387524c3621c5a72275 to your computer and use it in GitHub Desktop.
Conferir os métodos!
package matrizes;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
*
* @author bruce
*/
public class Matrizes {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
int[][] m = readMatrixFile("file.txt");
printMatriz(m);
System.out.println(max(m));
}
//Exercicio com discussão em duplas 1
static int[][] generateMatrix(int lin, int col) {
int[][] matriz = new int[lin][col];
for (int i = 0; i < matriz.length; i++) {
for (int j = 0; j < matriz[0].length; j++) {
matriz[i][j] = (int) (Math.random() * 100);
}
}
return matriz;
}
//Exercicio com discussão em duplas 2
static void printMatriz(int[][] m) {
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[0].length; j++) {
System.out.print(m[i][j] + " ");
}
//Vai pra linha de baixo a cada linha da matriz impressa
System.out.println("");
}
}
//Exercicio com discussão em duplas 3
static int max(int m[][]) {
int maior = m[0][0];
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[0].length; j++) {
if(m[i][j] > maior) {
maior = m[i][j];
}
}
}
return maior;
}
/*MANIPULAÇÃO DE ARQUIVOS*/
public static int[][] readMatrixFile(String file) throws IOException {
int[][] matriz;
BufferedReader entrada = new BufferedReader(new FileReader(file));
String linha = entrada.readLine();
String[] tamanhoMatriz = linha.split(" ");
int tamanhoLinha, tamanhoColuna;
tamanhoLinha = Integer.parseInt(tamanhoMatriz[0]);
tamanhoColuna = Integer.parseInt(tamanhoMatriz[1]);
matriz = new int[tamanhoLinha][tamanhoColuna];
for (int i = 0; i < tamanhoLinha; i++) {
linha = entrada.readLine();
String[] aux = linha.split(" ");
for (int j = 0; j < aux.length; j++) {
matriz[i][j] = Integer.parseInt(aux[j]);
}
}
return matriz;
}
//Exercicio com discussão em duplas 4
static int maxValueFromFile(String file) throws IOException {
int[][] matriz = readMatrixFile(file);
int maior = max(matriz);
return maior;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment