2013-02-18 28 views
6

我有一个用户输入名称列表的程序。我有一个开关箱,我希望按照字母顺序打印名称。按字母顺序列出字符串数组

public static void orderedGuests(String[] hotel) 
{ 
    //?? 
} 

我曾经尝试都

Arrays.sort(hotel); 
System.out.println(Arrays.toString(hotel)); 

java.util.Collections.sort(hotel); 
+3

什么不工作有关这些解决方案? – 2013-02-18 21:40:30

+0

发布整个代码。随着顺序,你正在做的一切。 – Maroun 2013-02-18 21:44:26

回答

11

奇怪,你的代码似乎为我工作:

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运行代码测试鲍比乔天使“,这里是输出:

$ java Test Bobby Joe Angel 
Angel 
Bobby 
Joe 
1

这里是代码的工作:

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)); 
    } 

} 
2

你可以使用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]); 
} 
4

您尝试的第一件事似乎正常工作。这是一个示例程序。
this page顶部的“开始”按钮以运行它自己查看输出。

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)); 
    } 
} 
1

java.util.Collections.sort(listOfCountryNames,Collat​​or.getInstance());

0
**//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]); 
} 
} 
}** 
1

按字母顺序我假设为了:A |一个< C | b <ç| C ... 希望这是@Nick是什么(或者是)寻找,答案如下上述假设。

我建议有一个类实现比较比较器的界面的方法:

public int compare(Object o1, Object o2) { 
    return o1.toString().compareToIgnoreCase(o2.toString()); 
} 

和调用的方法使用自定义比较调用Arrays.sort方法:

Arrays.sort(inputArray, customComparator); 

观察结果: 输入阵列:“Vani”,“Kali”,“Mohan”,“Soni”,“kuldeep”,“Arun”

输出(字母顺序)是: 阿伦,卡利,kuldeep,磨憨,瑞里,VANI

输出(自然顺序通过执行Arrays.sort(inputArray)为: 阿伦,卡利,磨憨,瑞里,瓦尼,kuldeep

因此,在壳体自然排序,[Vani < kuldeep]这对我的字母顺序的理解是不是所需的东西。

自然和字母/词汇顺序访问discussion here

0

阵列的更多的了解。排序(字符串数组); 这将根据Unicode字符值对字符串数组进行排序。在字符串的开头包含大写字符的所有字符串将位于排序列表的顶部,按字母顺序排列,后面跟随所有带有小写字符的字符串。 因此,如果数组包含字符串用大写字符和小写字符开头,数组排序不会返回区分大小写的顺序按字母顺序排列

String[] strArray = { "Carol", "bob", "Alice" }; 
Arrays.sort(strList); 
System.out.println(Arrays.toString(hotel)); 

输出是:爱丽丝,卡罗尔,鲍勃,

如果您要求对字符串进行排序而不考虑大小写,则需要为Arrays.sort()第二个参数比较器。这样的比较器已经为我们编写了,并且可以在名为CASE_INSENSITIVE_ORDER的String类上以静态方式进行访问。

String[] strArray = { "Carol", "bob", "Alice" }; 
Arrays.sort(stringArray, String.CASE_INSENSITIVE_ORDER); 
System.out.println(Arrays.toString(strArray)); 

输出是:爱丽丝,鲍勃,卡罗尔