Java数据库显示国家数据库表中的所有国家信息

Java数据库显示国家数据库表中的所有国家信息,java,arrays,class,sorting,methods,Java,Arrays,Class,Sorting,Methods,我正在处理一个Java任务,该任务要求更改Country类,以便它从国家数据库表中打印每个国家,而不仅仅是一个国家。我尝试了各种实例,但无法让它显示所有实例。我对Java非常陌生,所以我知道我不知怎么搞砸了语法,或者它就在我面前,我只是在这方面工作太久了,需要另一双眼睛。以下是我迄今为止从国家表中显示的一个国家 public class CountryMain { public static void main(String[] args) { ReadCount

我正在处理一个Java任务,该任务要求更改Country类,以便它从国家数据库表中打印每个国家,而不仅仅是一个国家。我尝试了各种实例,但无法让它显示所有实例。我对Java非常陌生,所以我知道我不知怎么搞砸了语法,或者它就在我面前,我只是在这方面工作太久了,需要另一双眼睛。以下是我迄今为止从国家表中显示的一个国家

    public class CountryMain {
    public static void main(String[] args) {
        ReadCountryDB rcdb = new ReadCountryDB();
        List<Country> countries = rcdb.getCountries();

        Country firstCountry = countries.get(0);
        System.out.println("First country:");
        System.out.println("Name: " + firstCountry.getName()
                           + "  Population: " + firstCountry.getPopulation()
                           + "  Median Age: " + firstCountry.getMedianAge());
      }
}
公共类CountryMain{
公共静态void main(字符串[]args){
ReadCountryDB rcdb=新的ReadCountryDB();
List countries=rcdb.getCountries();
Country firstCountry=countries.get(0);
System.out.println(“第一个国家:”);
System.out.println(“名称:”+firstCountry.getName()
+“人口:”+firstCountry.getPopulation()
+“中位年龄:”+firstCountry.getMedianAge());
}
}
我知道我需要更改“Country firstCountry=countries.get(0)”方法部分,我尝试了未定义的getAll,但我对需要定义的内容感到困惑,因此它从国家数据库中提取所有内容


非常感谢您在这方面提供的任何帮助。

如果您已经通过
rcdb.getCountries()
提取了所有国家,那么只需使用循环打印出所有国家:

for (Country country : countries) {   
     System.out.println("Country:");
     System.out.println("Name: " + country.getName()
                       + "  Population: " + country.getPopulation()
                       + "  Median Age: " + country.getMedianAge());
}
循环

   public class CountryMain {
    public static void main(String[] args) {
    ReadCountryDB rcdb = new ReadCountryDB();
    List<Country> countries = rcdb.getCountries();
    for(int i=0; i<coutries.size; i++){
     Country firstCountry = countries.get(i);
     System.out.println("First country:");
     System.out.println("Name: " + firstCountry.getName()
                       + "  Population: " + firstCountry.getPopulation()
                       + "  Median Age: " + firstCountry.getMedianAge());
    }
  }
}
公共类CountryMain{
公共静态void main(字符串[]args){
ReadCountryDB rcdb=新的ReadCountryDB();
List countries=rcdb.getCountries();

对于(int i=0;i如果您在
国家/地区列表中获得了正确的计数,那么您可以在列表中循环使用每个元素-

for (Country country : countries) {
  System.out.println("Name: " + country.getName()
                       + "  Population: " + country.getPopulation()
                       + "  Median Age: " + country.getMedianAge());
}

对循环使用