2015-05-13 69 views
1

我正在研究需要更改Country类的Java任务,以便从国家数据库表打印每个国家,而不仅仅是一个国家/地区。我已经尝试了各种实例,但无法显示它们。我对Java非常陌生,所以我知道我在某种程度上搞乱了语法,或者它公然地在我面前,而我刚刚在这方面工作太久,需要另一套眼睛。这是我迄今为止在国家表中显示的一个国家。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()); 
     } 
} 

我知道它的“国家firstCountry = countries.get(0)”方法部分,我需要改变,我已经试过GETALL这是不确定的,但我对我需要什么困惑定义它从国家数据库中提取所有内容。

对此非常感谢。

+0

使用一个'for循环'? –

回答

0

如果您已经退出所有国家通过rcdb.getCountries(),那么就使用一个循环来打印出所有国家:

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

循环它

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

如果你正在在正确的计数countries列表,然后你可以通过列表循环来处理每个元素 -

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