如何让国家名单按字母顺序排列,名称正确
how to get countries list alphabetically arranged with the correct names
我正在尝试查找所有国家/地区的名称。
我试图从 java.util.Locale
获取它们,但我发现有些国家的名称有误,而且列表没有按字母顺序排列。
从 java.util.Locale
获取列表我使用了这个代码
private List<String> countriesList = new ArrayList<String>();
public List<String> getCountriesList() {
String[] locales = Locale.getISOCountries();
for (String countryCode : locales) {
Locale obj = new Locale("", countryCode);
countriesList.add(obj.getDisplayCountry(Locale.FRENCH));
}
return countriesList;
}
那么有没有办法让它们以正确的名称按字母顺序排列?
我认为有两个不同的问题:
- 列表未按字母顺序排列。
您只需要对数组进行排序。有很多方法可以做到这一点,其中之一可能如下:
Collections.sort(countriesList, new Comparator<String>() {
@Override
public int compare(String str1, String Str2)
{
return str1.compareTo(str2);
}
});
- 部分国家名称错误
如果有些国家/地区的名称有误,尤其是在您的语言中,将很难在 Java 中对其进行任何处理。如果您不能依赖内部 Java 国家/地区名称,您可以将名称列表直接放入您的应用程序 "manually",这可能很烦人,或者例如使用网络服务。有很多,例如检查这个one。这种方法的好处是,如果出现一些新的国家,列表可能会更新,有时会发生这种情况。
Just use Collections.sort(listName) and your list will get sorted
List<String> countriesList = new ArrayList<String>();
String[] locales = Locale.getISOCountries();
for (String countryCode : locales) {
Locale obj = new Locale("", countryCode);
countriesList.add(obj.getDisplayCountry(Locale.FRENCH));
Collections.sort(countriesList);
}
for(String s:countriesList)
{
System.out.println(s);
}
In your code just add sort method before the return statement of the method
private List<String> countriesList = new ArrayList<String>();
public List<String> getCountriesList() {
String[] locales = Locale.getISOCountries();
for (String countryCode : locales) {
Locale obj = new Locale("", countryCode);
countriesList.add(obj.getDisplayCountry(Locale.FRENCH));
}
Collections.sort(countriesList);
return countriesList;
}
我正在尝试查找所有国家/地区的名称。
我试图从 java.util.Locale
获取它们,但我发现有些国家的名称有误,而且列表没有按字母顺序排列。
从 java.util.Locale
获取列表我使用了这个代码
private List<String> countriesList = new ArrayList<String>();
public List<String> getCountriesList() {
String[] locales = Locale.getISOCountries();
for (String countryCode : locales) {
Locale obj = new Locale("", countryCode);
countriesList.add(obj.getDisplayCountry(Locale.FRENCH));
}
return countriesList;
}
那么有没有办法让它们以正确的名称按字母顺序排列?
我认为有两个不同的问题:
- 列表未按字母顺序排列。
您只需要对数组进行排序。有很多方法可以做到这一点,其中之一可能如下:
Collections.sort(countriesList, new Comparator<String>() {
@Override
public int compare(String str1, String Str2)
{
return str1.compareTo(str2);
}
});
- 部分国家名称错误
如果有些国家/地区的名称有误,尤其是在您的语言中,将很难在 Java 中对其进行任何处理。如果您不能依赖内部 Java 国家/地区名称,您可以将名称列表直接放入您的应用程序 "manually",这可能很烦人,或者例如使用网络服务。有很多,例如检查这个one。这种方法的好处是,如果出现一些新的国家,列表可能会更新,有时会发生这种情况。
Just use Collections.sort(listName) and your list will get sorted
List<String> countriesList = new ArrayList<String>();
String[] locales = Locale.getISOCountries();
for (String countryCode : locales) {
Locale obj = new Locale("", countryCode);
countriesList.add(obj.getDisplayCountry(Locale.FRENCH));
Collections.sort(countriesList);
}
for(String s:countriesList)
{
System.out.println(s);
}
In your code just add sort method before the return statement of the method
private List<String> countriesList = new ArrayList<String>();
public List<String> getCountriesList() {
String[] locales = Locale.getISOCountries();
for (String countryCode : locales) {
Locale obj = new Locale("", countryCode);
countriesList.add(obj.getDisplayCountry(Locale.FRENCH));
}
Collections.sort(countriesList);
return countriesList;
}