0
開始編寫這個簡單的程序後,我遇到了一個我很想解釋的邏輯錯誤。該toString()
方法正在打印[email protected]
使用.toString顯示ArrayList
測試講座
public static void main(String[] args) {
GeographyList g = new GeographyList();
g.addCountry ("Scotland");
g.addCountry ("Wales");
g.addCountry ("Ireland");
g.addCountry ("Italy");
System.out.println(g.toString());
ArrayList的設置
public class GeographyList {
private ArrayList<String> countries;
public GeographyList(){
countries = new ArrayList<>();
}
public ArrayList<String> getCountries() {
return countries;
}
public String getCountry(int index){
return countries.get(index);
}
public void addCountry(String aCountry){
countries.add(aCountry);
System.out.println(aCountry + " added.");
}
在你的類中實現'toString()'來獲得你想要的輸出。 –
'g.toString()'將打印'g'對象的引用 – Ali
謝謝,花了很多年看這個明顯的錯誤:)。 – eGathergood