Ordenação de matriz [][] Complicado

2 respostas
B

Olá pessoal estou pedindo um ajuda nesse código, estou a quase um mes tentando ordenar os numeros mas não estou conseguindo poderiam dar uma verificada no código abaixo e ver o que esta errado pois não compila e não ordena tb. :frowning:

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

      int matriznum[][] = new int[10][6];
            
      int maximo = 60;
      int troca;
	// preenche a matriz e gera os numeros aleatórios
	for (int j=0; j < matriznum.length; j++)
	{
	   for (int k=0; k < matriznum[j].length; k++)
	   {
	        matriznum[j][k] = j*k;
		matriznum[j][k] = (int) (Math.random() * maximo);
           }

	}


	// Ordenação

	for (int j=0; j < matriznum.length; j++)
	{
	   for (int k=0; k < matriznum[j].length - 1; k > j; k++)
	   {
	     if( matriznum[j] < matriznum[k] )
		troca( matriznum, j, k);

           }

	}


	for (int j=0; j < matriznum.length; j++)
	{
	   for (int k=0; k < matriznum[j].length; k++)
	   {
	        
		System.out.print(matriznum[j][k]+ " , ");
	   		
	   }
	   System.out.println();
	}
   }
}

2 Respostas

Rafael_Steil

A maneira mais facil eh usar o metodo sort(int[]) da classe Arrays:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#sort(int[])

Eis um exemplo:

import java.util.Arrays;

public class Teste {
	public static void main(String[] args) {
		int numeros[][] = new int[10][6];
		int maximo = 60;

		for (int i = 0; i < numeros.length; i++) {
			for (int j = 0; j < numeros[i].length; j++) {
				numeros[i][j] = (int)(Math.random() * maximo);
			}
		}

		for (int i = 0; i < numeros.length; i++) {
			Arrays.sort(numeros[i]);
		}

		for (int i = 0; i < numeros.length; i++) {
			for (int j = 0; j < numeros[i].length; j++) {
				System.out.print(numeros[i][j] + ", ");
			}

			System.out.println();
		}
	}
}

Rafael

B

Obrigada Rafael agora não esqueço mais como fazer.

Criado 5 de junho de 2005
Ultima resposta 5 de jun. de 2005
Respostas 2
Participantes 2