0

每次我在 Netbeans 中编译我的代码时,我都会将其作为输出:

com.logger.ErrorFrequency@1357842

而且我不知道它是什么或如何使代码给我预期的结果。我用谷歌搜索无济于事并检查了我的代码。但我看不出有什么问题。

这是我的代码:

package com.logger;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TestA {
    static final String url = "jdbc:mysql://localhost:3306/monitor";

    final String error;
    final Date sta;

    //initialize constructor
    public TestA(){
        super();
        this.error = null;
        this.sta = null;
    }
    public TestA(String p, Date n) {
        error = p;
        sta = n;
    }

    public String toString() {
        return error + ", " + sta;
    }

    public String getReport(){
        return error;
    }
    public Date getSta(){
        return sta;
    }


    public List getErrDesc(){
        //String data = null;
        Date date = null;
        List<TestA> list = new ArrayList<TestA>();
        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(url, "root", "");

            Statement stmt = con.createStatement();

            ResultSet result = stmt.executeQuery("SELECT error_description, error_date     FROM error_log WHERE service_source = 'Billbox' ");

            while (result.next()) {  //retrieve data

                String ds = result.getString("error_date");
                date = new SimpleDateFormat("y/M/d h:m:s a").parse(ds);
                TestA es = new TestA(result.getString("error_description"), date);
                list.add(es);

                //System.out.println(data);
            }
            con.close();

        } catch (Exception ex) {
            Logger.getLogger(LogDB.class.getName()).log( 
                            Level.SEVERE, null, ex);
        }
        return list;
  }   
}

然后这是第二节课:

package com.logger;

import java.text.SimpleDateFormat;
import java.util.*;

class ErrorFrequency  {
    private final List errStamp;


    public ErrorFrequency(List ae) {
        errStamp = ae;

    }

    public List getReport(){
        return errStamp;
    }



    public static final void main(String[] args) {
        PriorityQueue<ErrorFrequency> pq = new PriorityQueue<ErrorFrequency>();

        TestA element = new TestA();
        List item = element.getErrDesc();
        ErrorFrequency e = new ErrorFrequency(item);

        pq.add(e);
        ErrorFrequency peek = pq.peek();
        System.out.println(peek.toString());
        System.out.println(pq.size());

    }
}
4

1 回答 1

1

这是因为peek.toString()如果您想将其用于输出,则需要覆盖它。

默认toString()方法显示对象类及其哈希码,以便您可以区分不同的对象实例。

com.logger.ErrorFrequency@1357842

实际上是

类@哈希码

你可以做些什么来打印你的报告是这样的:

for(Object o: peek.getReport()) {
    // Do stuff with your object here.
    // I don't know what type your list
    // If it is a list of strings you can use this:
    System.out.println((String)o);
}
于 2013-07-10T08:52:06.013 回答