Zeigt eine Liste von Ländern in Java an

Eine Liste der Länder in Java anzeigen

world-country

In diesem Artikel zeigen wir Ihnen, wie Sie die KlasseLocaleverwenden, um die Liste der Länder zu umgehen.

P.S Tested with JDK 1.6

1. Liste der Länder

DieLocale.getISOCountries() geben eine Liste aller inISO 3166 definierten 2-Buchstaben-Ländercodes zurück.

ListCountry.java

package com.webmitta.model;

import java.util.Locale;

public class ListCountry {

    public static void main(String[] args) {

    ListCountry obj = new ListCountry();
    obj.run();

    }

    public void run() {

    String[] locales = Locale.getISOCountries();

    for (String countryCode : locales) {

        Locale obj = new Locale("", countryCode);

        System.out.println("Country Code = " + obj.getCountry()
            + ", Country Name = " + obj.getDisplayCountry());

    }

    System.out.println("Done");
    }

}

Ausgabe

Country Code = AD, Country Name = Andorra
Country Code = AE, Country Name = United Arab Emirates
Country Code = AF, Country Name = Afghanistan
Country Code = AG, Country Name = Antigua and Barbuda
Country Code = AI, Country Name = Anguilla
Country Code = AL, Country Name = Albania
//skip ...

2. Liste der Länder in definierten Sprachen

Rufen Sie eine Liste der Länder ab und zeigen Sie den Ländernamen auf Chinesisch an.

ListCountry.java

package com.webmitta.model;

import java.util.Locale;

public class ListCountry {

    public static void main(String[] args) {

    ListCountry obj = new ListCountry();

    //get a list of countries and display in Chinese
    obj.getListOfCountries(Locale.Chinese);

    //display in frence
    //obj.getListOfCountries(Locale.FRENCH);

    }

    public void getListOfCountries(Locale locale) {

    String[] locales = Locale.getISOCountries();

    for (String countryCode : locales) {

        Locale obj = new Locale("", countryCode);

        System.out.println("Country Code = " + obj.getCountry()
        + ", Country Name = " + obj.getDisplayCountry(locale));

     }

    }

}

Ausgabe, Eclipse mit aktivierter UTF-8-Ausgabe

country list

3. Liste der Länder in eigenen Sprachen

Rufen Sie eine Liste der Länder ab und zeigen Sie den Ländernamen in den Sprachen des jeweiligen Landes an. Der Trick besteht darin, einMap-Objekt zu erstellen und den Ländercode und die Landessprachen für eine spätere Abfrage zu speichern.

Note
Achtung, nicht alle Länder haben eigene Sprachen.

ListCountry.java

package com.webmitta.model;

import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

public class ListCountry {

    private Map languagesMap = new TreeMap();

    public ListCountry() {
    initLanguageMap();
    }

    public static void main(String[] args) {

    ListCountry obj = new ListCountry();
    obj.getListOfCountries();

    }

    public void getListOfCountries() {

    String[] countries = Locale.getISOCountries();

    int supportedLocale = 0, nonSupportedLocale = 0;

    for (String countryCode : countries) {

      Locale obj = null;
      if (languagesMap.get(countryCode) == null) {

        obj = new Locale("", countryCode);
        nonSupportedLocale++;

      } else {

        //create a Locale with own country's languages
        obj = new Locale(languagesMap.get(countryCode), countryCode);
        supportedLocale++;

      }

      System.out.println("Country Code = " + obj.getCountry()
        + ", Country Name = " + obj.getDisplayCountry(obj)
        + ", Languages = " + obj.getDisplayLanguage());

      }

      System.out.println("nonSupportedLocale : " + nonSupportedLocale);
      System.out.println("supportedLocale : " + supportedLocale);

    }

    // create Map with country code and languages
    public void initLanguageMap() {

    Locale[] locales = Locale.getAvailableLocales();

    for (Locale obj : locales) {

      if ((obj.getDisplayCountry() != null) && (!"".equals(obj.getDisplayCountry()))) {
        languagesMap.put(obj.getCountry(), obj.getLanguage());
      }

    }

    }

}

Ausgabe, Eclipse mit aktivierter UTF-8-Ausgabe

country list