【问题标题】:Object Array won't print correctly [duplicate]对象数组无法正确打印 [重复]
【发布时间】:2013-05-11 03:02:21
【问题描述】:

我是编程和 Java 的新手,我正在做一个指导非常严格的学校项目。我确信有一种更有效的方法来编写我的代码,但这不是我的问题。当我尝试在 main 底部打印我的数组时,我得到了

"Country@10b28f30, Country@3ad6a0e0, Country@60dbf04d",....等等。

我知道数组正在加载,因为当 // System.out.println("对象为:" + 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

【问题讨论】:

  • 重写 Country 类中的 toString 方法,返回一个字符串,提供满足您需求的对象的表示形式
  • 老实说,我不知道该怎么做。

标签: java arrays object printing tostring


【解决方案1】:

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

p.s:在这两种情况下,您都必须重写 toString 实现。

【讨论】:

    【解决方案2】:

    默认情况下,每个类都有一个 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]
    

    【讨论】:

    • 好的,我现在明白了。非常感谢
    • [LCountry;@60dbf04d [LCountry;@60dbf04d [LCountry;@60dbf04d [LCountry;@60dbf04d [LCountry;@60dbf04d [LCountry;@60dbf04d [LCountry;@60dbf04d ....] >
    • @user2368407 更新了工作示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多