دنبال کننده ها

۱۳۹۶ اسفند ۲۸, دوشنبه

Sort array of type Two-dimensional String in alphabetical order in Java

[ad_1]



I'm doing an exercise but I get an error when I get to the line where it is ordered, I'm sorting it with the sort function, here the exercise, I already made a first version that I leave here, and I also resolved how to order them but he just orders me the rows, and I want him to order me by row and columns, thank you



Exercise:



Capture a matrix that allows me to store names of people, show the
original matrix and
Sort them in alphabetical order.


Code



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

public class NombresPersonas

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

//array a ordenar
//String cadenas[]="Fernando","Pepe","Alejandro","Alfredo","Eufrasio";


System.out.println("Ingrese el numero de filas: ");
int filas = sc.nextInt();

System.out.println("Ingrese el numero de columnas: ");
int columnas = sc.nextInt();

String cadenas[][] = new String [filas][columnas];

for (int i = 0; i < cadenas.length; i++)
for (int j = 0; j < cadenas[0].length; j++)
System.out.println("Ingrese un nombre para la casilla [" + i + "][" + j + "]");
cadenas[i][j] = sc.next();



Arrays.sort(cadenas);

//Mostramos el array ya ordenado
for (String i[] : cadenas)
for(String k: i)
System.out.println(i);






Error



Ingrese el numero de filas: 
2
Ingrese el numero de columnas:
2
Ingrese un nombre para la casilla [0][0]
pedro
Ingrese un nombre para la casilla [0][1]
jesus
Ingrese un nombre para la casilla [1][0]
alfonso
Ingrese un nombre para la casilla [1][1]
carlos
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String;
cannot be cast to java.lang.Comparable
at
java.util.ComparableTimSort.countRunAndMakeAscending
(ComparableTimSort.java:32 )

at java.util.ComparableTimSort.sort(ComparableTimSort.java:188)
at java.util.Arrays.sort(Arrays.java:1246)
at NombresPersonas.main(NombresPersonas.java:29)
C:UsersJosé PadrónAppDataLocalNetBeansCache8.2executor-
snippetsrun.xml:53: Java returned: 1
BUILD FAILED (total time: 8 seconds)


What am I doing wrong?



PS: I just made an improvement to the code but it just orders me the names by rows



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

public class NombresPersonas

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

//array a ordenar
//String cadenas[]="Fernando","Pepe","Alejandro","Alfredo","Eufrasio";


System.out.println("Ingrese el numero de filas: ");
int filas = sc.nextInt();

System.out.println("Ingrese el numero de columnas: ");
int columnas = sc.nextInt();

String cadenas[][] = new String [filas][columnas];

for (int i = 0; i < cadenas.length; i++)
for (int j = 0; j < cadenas[0].length; j++)
System.out.println("Ingrese un nombre para la casilla [" + i + "][" + j + "]");
cadenas[i][j] = sc.next();



System.out.println("Matriz original: ");
imprimir(cadenas);

for(String[] i: cadenas)
Arrays.sort(i);


System.out.println("Matriz ordenada alfábeticamente: ");
imprimir(cadenas);



public static void imprimir(String M[][])
System.out.println();
for (int i=0; i<M.length; i++)
for (int j=0; j<M[0].length; j++)
System.out.print(M[i][j]+" ");
System.out.println();

System.out.println();






[ad_2]

لینک منبع