我是编程和Java的新手,我正在做一个有严格指导的学校项目。我确信有一种更有效的方法来执行我的代码,但这不是我的问题。当我尝试在main的底部打印我的数组时,我得到了
“Country @ 10b28f30,Country @ 3ad6a0e0,Country @ 60dbf04d”,....等等。
我知道数组正在加载,因为何时 // System.out.println(“object is:”+ name +“”+ capital +“”+ region +“”+ region_Nbr +“”+ capital_population);
运行,它会在构建时打印数组的所有元素。我一直在读点什么 关于必须覆盖toString,我已经多种方式来打印数组,没有工作。提前谢谢。
public class Main {
/**
* @param args the command line arguments
*/
private int size = 43;
private static Country[] countryInfo = new Country[43];
private Control control;
public static void main(String[] args) throws IOException {
String name = "";
String capital = "";
String region = "";
int region_Nbr = 0;
int capital_population = 0;
// TODO code application logic here
String filename = "Countries.txt";
String inputString;
FileInputStream fis1 = new FileInputStream(filename);
BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1));
inputString = br1.readLine();
int count = 0;
while (inputString != null) {
//System.out.print(inputString + "\n");
name = inputString.substring(0, 13).trim();
//System.out.print(name + ", "); //echo
capital = inputString.substring(24, 36).trim();
//System.out.print(capital + ", ");//echo
region = inputString.substring(40, 56).trim();
//System.out.print(region + ", "); //echo
region_Nbr = Integer.parseInt(inputString.substring(64, 66).trim());
//System.out.print(region_Nbr + ", ");//echo
capital_population = Integer.parseInt(inputString.substring(72, inputString.length()).trim());
//System.out.print(capital_population + "\n");
countryInfo[count] = new Country(name, capital, region, region_Nbr, capital_population);
//Control.printArray(countryInfo);
inputString = br1.readLine();
count++;
} //end while
br1.close();
System.out.println(Arrays.toString(countryInfo));
}
}// end class Main
import java.util.Arrays;
public class Country
{
private String name;
private String capital;
private String region;
private int region_Nbr;
private int capital_population;
private Control control;
public Country (String strName, String strCapital,String strRegion, int iregion_Nbr, int icapitalpop)
{
name = strName;
capital = strCapital;
region = strRegion;
region_Nbr = iregion_Nbr;
capital_population = icapitalpop;
// System.out.println ("object is: " + name + " " + capital + " " + region + " " + region_Nbr + " " + capital_population);
}// end constructor
}//end class
}//end class
答案 0 :(得分:0)
默认情况下,每个类都有一个toString方法。您可以覆盖此方法以返回更有意义的String值。
import java.util.Arrays;
public class Country
{
private String name;
private String capital;
private String region;
private int region_Nbr;
private int capital_population;
private Control control;
public Country (String strName, String strCapital,String strRegion, int iregion_Nbr, int icapitalpop)
{
name = strName;
capital = strCapital;
region = strRegion;
region_Nbr = iregion_Nbr;
capital_population = icapitalpop;
// System.out.println ("object is: " + name + " " + capital + " " + region + " " + region_Nbr + " " + capital_population);
}// end constructor
public String toString() {
return "Country: name = " + name + "; capital = " + capital + "; region = " + region + "; regionNbr = " + region_Nbr + "; population = " + capital_population;
}
}//end class
显然你可以按照你想要的方式格式化依赖
import java.io.IOException;
import java.util.Arrays;
import java.util.ResourceBundle.Control;
public class Main {
private static Country[] countryInfo = new Country[1];
public static void main(String[] args) throws IOException {
new Main();
}
public Main() {
String name = "";
String capital = "";
String region = "";
int region_Nbr = 0;
int capital_population = 0;
countryInfo[0] = new Country("Australia", "Canberra", "AU", 61, 6000000);
System.out.println(Arrays.toString(countryInfo));
}
public class Country {
private String name;
private String capital;
private String region;
private int region_Nbr;
private int capital_population;
private Control control;
public Country(String strName, String strCapital, String strRegion, int iregion_Nbr, int icapitalpop) {
name = strName;
capital = strCapital;
region = strRegion;
region_Nbr = iregion_Nbr;
capital_population = icapitalpop;
}// end constructor
@Override
public String toString() {
return "Country: name = " + name + "; capital = " + capital + "; region = " + region + "; regionNbr = " + region_Nbr + "; population = " + capital_population;
}
}//end class
}//end class
...输出
[Country: name = Australia; capital = Canberra; region = AU; regionNbr = 61; population = 6000000]
答案 1 :(得分:0)
由于您希望打印自定义类的元素,即您的国家/地区,您需要覆盖Country类中的toString实现。 如果您没有覆盖自定义类的toString,它将为您打印引用。
@Override
public String toString() {
return "Country: name = " + this.name + "; capital = " + this.capital + "; region = " + this.region ;
}
你为什么不用这个
List<Country> countryInfoList = new ArrayList<Country>();
.....
and then just add the country to the countryInfoList i.e countryInfoList.add(country);
p.s:在这两种情况下,您都必须覆盖toString实现。