除了“Collections.Sort(list)”方法之外,他们是另一种对用户在输出框中单独输入的名称列表进行排序的方法吗?
import java.util.*;
public class UserInput
{
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Scanner stdin = new Scanner(System.in);
do {
System.out.println("Enter a name: ");
String strin = stdin.next();
if (isNumeric(strin)) {
System.out.println("Please add a non numeric value");
} else {
list.add(strin);
}
Collections.sort(list);
System.out.println("Current list is " + list);
System.out.println("Add more? (y/n)");
} while (stdin.next().startsWith("y"));
System.out.println("List is " + list);
String[] arr = list.toArray(new String[0]);
System.out.println("Array is " + Arrays.toString(arr));
}
public static boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}
}