Jump to content

Recommended Posts

Posted (edited)

então como e que percorre a matriz e imprimo os resultados?

estar a responder a isso é fazer o trabalho todo, não é ?

fica só um exemplo de uma classe para manusear uma matriz (não é genérica para não complicar):

class Matrix {
 private int width;
 private int height;
 private int[][] matrix;

 public Matrix(int width, int height) {
   if (width == 0 || height == 0)
     throw IllegalArgumentException("valor de tamanho inválido de zero");

   this.width = width;
   this.height = height;
   this.matriz = new int[this.width][this.height];
 }

 public int getWidth() {
   return this.width;
 }

 public int getHeight() {
   return this.height;
 }

 public int getCell(int x, int y) {
   if (this.x >= this.width || this.y >= this.height)
     return IndexOutOfBoundsException();

   return this.matrix[x][y];
 }

 public int[] row(int y) {
   if (this.y >= this.height)
     return IndexOutOfBoundsException();

   int[] result = new int[this.width];
   for (int i = 0; i < this.width; i++)
     result[i] = this.matrix[i][y];

   return result;
 }

 public int[] column(int x) {
   if (this.x >= this.width)
     return IndexOutOfBoundsException();

   int[] result = new int[this.height];
   for (int i = 0; i < this.height; i++)
     result[i] = this.matrix[x][i];

   return result;
 }
}
Edited by HappyHippyHippo
IRC : sim, é algo que ainda existe >> #p@p

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.