gpt4 book ai didi

java - 每当我编译时,输出都会引用内存中的某个位置(Java)

转载 作者:行者123 更新时间:2023-12-01 14:21:00 36 4
gpt4 key购买 nike

每次我在 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());

}
}

最佳答案

这是因为如果您想使用peek.toString()进行输出,则需要重写它。

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

com.logger.ErrorFrequency@1357842

实际上是

class@hashcode

您可以执行以下操作来打印报告:

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);
}

关于java - 每当我编译时,输出都会引用内存中的某个位置(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17566248/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com