-1

我是编程和 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
4

2 回答 2

0

默认情况下,每个类都有一个 toString 方法。您可以重写此方法以返回更有意义的字符串值。

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]
于 2013-05-11T03:14:22.437 回答
0

由于您想在您的情况下打印自定义类的元素,即 Country,您将需要覆盖 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);

ps:在这两种情况下,您都必须覆盖 toString 实现。

于 2013-05-11T03:56:48.133 回答