I tried to call this method from the Arrays class without using an import statement: Could u please tell me what´s wrong?
//import java.util.Scanner;
class ArraySorting {
/**
* @param array unordered sequence of strings
* @return ordered array of strings
*/
public static String[] sortArray(String[] array) {
// write your code here
java.util.Arrays.sort input = new java.util.Arrays.sort(System.in);
String string = input.next();
String[] str = string.split(" ");
java.util.Arrays.sort (array);
System.out.println(string.toString(array));
}
return (array);
}
public static void main(String[] args) {
sortArray();
}
}
Thanks alot
The method to sort the array should be cleaned out of the redundant code and has to be as simple as:
public static String[] sortArray(String[] array) {
java.util.Arrays.sort (array);
System.out.println(java.util.Arrays.toString(array));
return array;
}
Next, the test must be rewritten to read a line, split into arrays of strings to be sorted with sortArray
:
public static void main(String ... args) {
java.util.Scanner input = new java.util.Scanner(System.in);
String string = input.nextLine(); // read entire line not a single token
String[] str = string.split(" ");
System.out.println("before sort: " + java.util.Arrays.toString(str));
String[] sorted = sortArray(str);
System.out.println("str: " + java.util.Arrays.toString(str));
System.out.println("sorted: " + java.util.Arrays.toString(str));
}
before sort: [abc, aad, 0123, 456, zxy, aabc]
[0123, 456, aabc, aad, abc, zxy]
str: [0123, 456, aabc, aad, abc, zxy]
sorted: [0123, 456, aabc, aad, abc, zxy]
Note: Arrays.sort
modifies its input array so actually method sortArray
may be void
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.