【问题标题】:List an Array of Strings in alphabetical order按字母顺序列出字符串数组
【发布时间】:2013-02-03 10:27:33
【问题描述】:
我有一个程序让用户输入姓名列表。我有一个 switch case 去一个函数,我想让名字按字母顺序打印出来。
public static void orderedGuests(String[] hotel)
{
//??
}
我都试过了
Arrays.sort(hotel);
System.out.println(Arrays.toString(hotel));
和
java.util.Collections.sort(hotel);
【问题讨论】:
标签:
java
arrays
function
case
【解决方案1】:
奇怪,你的代码似乎对我有用:
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
【解决方案2】:
您尝试的第一件事似乎工作正常。这是一个示例程序。
按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));
}
}
【解决方案3】:
您可以使用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】:
java.util.Collections.sort(listOfCountryNames, Collator.getInstance());
【解决方案5】:
这是有效的代码:
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));
}
}
【解决方案6】:
按字母顺序,我假设顺序是:A|a
我建议让一个类实现比较器接口的比较方法:
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”
输出(按字母顺序)是:
Arun、Kali、kuldeep、Mohan、Soni、Vani
输出(通过执行 Arrays.sort(inputArray) 的自然顺序是:
Arun、Kali、Mohan、Soni、Vani、kuldeep
因此,在自然排序的情况下,根据我对字母顺序的理解,[Vani
如需更多了解自然和字母/词汇顺序,请访问discussion here
【解决方案7】:
Arrays.sort(stringArray);
这会根据 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 ));
输出是:爱丽丝,鲍勃,卡罗尔
【解决方案8】:
您可以使用 Arrays.sort() 方法。这是一个例子,
import java.util.Arrays;
public class Test
{
public static void main(String[] args)
{
String arrString[] = { "peter", "taylor", "brooke", "frederick", "cameron" };
orderedGuests(arrString);
}
public static void orderedGuests(String[] hotel)
{
Arrays.sort(hotel);
System.out.println(Arrays.toString(hotel));
}
}
输出
[布鲁克,卡梅伦,弗雷德里克,彼得,泰勒]
【解决方案9】:
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");
}
}
}
【解决方案10】:
**//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]);
}
}
}**
【解决方案11】:
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;
}