Sort matrix ascending and descending

0

I have a question about sorting an array in ascending and descending order, I have seen the bubble method and I also found something about the 'sort' function but I do not know how to apply it, what should I know about it? I'm taking this now now but I'm throwing error

import java.util.Scanner;
import java.util.Arrays;

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

    Scanner sc = new Scanner(System.in);
    int x;
    int matriz[][];

    System.out.println("Ingrese la dimension de la matriz: ");
    x = sc.nextInt();
    matriz = new int[x][x];

    //Relleno de matriz automatico
    for (int i=0; i<x; i++){ 
        for (int j=0; j<x; j++) {
            matriz[i][j] = (int) (Math.random()*10);
        }
    }

    //Imprimir matriz original
    System.out.println("Matriz original");
    for (int i=0; i<x; i++){ 
        for (int j=0; j<x; j++){ 
            System.out.print(matriz[i][j]+" "); 
        }
        System.out.println(); 
    }

    //Imprimir diagonal principal
    System.out.print("La diagonal principal es:    ["); 
    for (int i=0; i<x; i++) 
        for (int j=0; j<x; j++){ 
            if (i == j) 
                System.out.print(matriz[i][j] +" "); 
        } 
    System.out.println("]");

    //Imprimir diagonal secundaria
    System.out.print("La diagonal secundaria es:    ["); 
    for (int i=x-1; i>=0; i--) 
        for (int j=0; j<x; j++) 
            if (x-1-i == j) 
                System.out.print(matriz[i][j] +" "); 
    System.out.println("]"); 

    Arrays.sort(matriz);
                                      //Esta parte es donde me tira el error
    for(int[] s : matriz)
        System.out.println(s);

    //Matriz ordenada de menor a mayor
    System.out.println("Matriz ordenada de forma ascendente");
    for (int i=0; i<x; i++){ 
        for (int j=0; j<x; j++){ 
            System.out.print(matriz[i][j]+" "); 
        }
        System.out.println(); 
    }
 }
}

The error is:

Ingrese la dimension de la matriz: 
3

9 3 7 
7 1 8 
0 5 6 

La diagonal principal es:    [9 1 6 ]
La diagonal secundaria es:    [0 1 7 ]
Exception in thread "main" java.lang.ClassCastException: [I cannot be cast 
to java.lang.Comparable
at
java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320 
 )
at java.util.ComparableTimSort.sort(ComparableTimSort.java:188)
at java.util.Arrays.sort(Arrays.java:1246)
at Matriz.main(Matriz.java:43)
C:\Users\José Padrón\AppData\Local\NetBeans\Cache.2\executor- 
snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)
    
asked by darioxlz 19.03.2018 в 01:43
source

2 answers

1

It is easier to visualize a two-dimensional arrangement if you imagine it as one of a dimension, but each of its elements contains another arrangement. For example, to fix your code you can do this:

import java.util.*;

public class Matriz{
public static void main(String[] args) {
    int[][] matriz = new int[10][10];

    //añade valores al arreglo
    for (int i = 0; i < matriz.length; i++){ 
        for (int j=0; j< matriz[i].length; j++) {
            matriz[i][j] = (int) (Math.random()*10);
        }
    }

    //accede a cada arreglo dentro de la matriz y lo pasas a la función sort que solo admite arreglos de una dimensión.
    for(int[] i: matriz){
        Arrays.sort(i);
    }
}

}

    
answered by 20.03.2018 / 02:54
source
0

The answer is something simple, Arrays.sort() only serves for arrays of a dimension, what you can do, understanding that a 2-dimensional array is as if each in one of the positions of a one-dimensional array had another one-dimensional array , is to make a Arrays.sort() of for example matrix [1], matrix [2], ... until the last position and then sort it based on the data of matrix [1] [1], matrix [2] [1] , ... to the last position equally

That is:

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

and then by the method of your preference order the arrangement based on the data of the positions [k] [1] of the array, with k = {0,1,2,3 ...., matrix.length -1}.

    
answered by 19.03.2018 в 02:52