我有一个程序,用户可以输入一个名字列表。我使用了一个开关语句调用一个函数,想要让这些名字按照字母顺序打印出来。
public static void orderedGuests(String[] hotel)
{
//??
}
我已经尝试过两种方法。
Arrays.sort(hotel);
System.out.println(Arrays.toString(hotel));
和
java.util.Collections.sort(hotel);
public static void orderedGuests(String[] hotel)
{
//??
}
我已经尝试过两种方法。
Arrays.sort(hotel);
System.out.println(Arrays.toString(hotel));
和
java.util.Collections.sort(hotel);
奇怪,你的代码在我这里似乎可以工作:
import java.util.Arrays;
public class Test
{
public static void main(String[] args)
{
// args is the list of guests
Arrays.sort(args);
for(int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
我使用"java Test Bobby Joe Angel"运行了那段代码,以下是输出结果:
$ java Test Bobby Joe Angel
Angel
Bobby
Joe
Arrays.sort(stringArray); 这个方法基于Unicode字符的值来对字符串数组进行排序。所有以大写字母开头的字符串将按字母表顺序排在排序列表的顶部,接着是所有以小写字母开头的字符串。 因此,如果数组包含以大写字母和小写字母开头的字符串,排序后的数组不会返回一个大小写不敏感的字母表顺序列表。
String[] strArray = { "Carol", "bob", "Alice" };
Arrays.sort(strList);
System.out.println(Arrays.toString(hotel));
输出结果为:Alice,Carol,bob。
如果您需要按照不区分大小写的方式对字符串进行排序,则需要为Arrays.sort()提供第二个参数Comparator。这样的比较器已经为我们编写好,并且可以作为String类的静态属性CASE_INSENSITIVE_ORDER来访问。
String[] strArray = { "Carol", "bob", "Alice" };
Arrays.sort(stringArray, String.CASE_INSENSITIVE_ORDER);
System.out.println(Arrays.toString(strArray ));
输出为:Alice,bob,Carol
import java.util.Arrays;
public class Foo{
public static void main(String[] args) {
String [] stringArray = {"ab", "aB", "c", "0", "2", "1Ad", "a10"};
orderedGuests(stringArray);
}
public static void orderedGuests(String[] hotel)
{
Arrays.sort(hotel);
System.out.println(Arrays.toString(hotel));
}
}
Arrays#sort()
,它能完美地工作。看这个例子:String [] a = {"English","German","Italian","Korean","Blablablabla.."};
//before sort
for(int i = 0;i<a.length;i++)
{
System.out.println(a[i]);
}
Arrays.sort(a);
System.out.println("After sort :");
for(int i = 0;i<a.length;i++)
{
System.out.println(a[i]);
}
java.util.Collections.sort(listOfCountryNames, Collator.getInstance());
import java.util.Arrays;
import java.util.Collections;
public class Test
{
public static void main(String[] args)
{
orderedGuests1(new String[] { "c", "a", "b" });
orderedGuests2(new String[] { "c", "a", "b" });
}
public static void orderedGuests1(String[] hotel)
{
Arrays.sort(hotel);
System.out.println(Arrays.toString(hotel));
}
public static void orderedGuests2(String[] hotel)
{
Collections.sort(Arrays.asList(hotel));
System.out.println(Arrays.toString(hotel));
}
}
CompareTo()
方法:基于Unicode字符值比较两个字符串。
import java.util.*;
public class Test {
int n,i,temp;
String names[n];
public static void main(String[] args) {
String names[5] = {"Brian","Joshua","Louis","David","Marcus"};
for(i=0;i<5;i++){
for(j=i+1;i<n;j++){
if(names[i].CompareTo(names[j]>0) {
temp=names[i];
names[i]=names[j];
names[j]=temp;
} else
System.out.println("Alphabetically Ordered");
}
}
}
public int compare(Object o1, Object o2) {
return o1.toString().compareToIgnoreCase(o2.toString());
}
并且从调用方法中使用自定义比较器调用Arrays.sort方法:
Arrays.sort(inputArray, customComparator);
**//With the help of this code u not just sort the arrays in alphabetical order but also can take string from user or console or keyboard
import java.util.Scanner;
import java.util.Arrays;
public class ReadName
{
final static int ARRAY_ELEMENTS = 3;
public static void main(String[] args)
{
String[] theNames = new String[5];
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the names: ");
for (int i=0;i<theNames.length ;i++ )
{
theNames[i] = keyboard.nextLine();
}
System.out.println("**********************");
Arrays.sort(theNames);
for (int i=0;i<theNames.length ;i++ )
{
System.out.println("Name are " + theNames[i]);
}
}
}**
public static String[] textSort(String[] words) {
for (int i = 0; i < words.length; i++) {
for (int j = i + 1; j < words.length; j++) {
if (words[i].compareTo(words[j]) > 0) {
String temp = words[i];
words[i] = words[j];
words[j] = temp;
}
}
}
return words;
}