gpt4 book ai didi

java - toString 方法不打印 HashMap 对象的文本表示

转载 作者:行者123 更新时间:2023-12-02 11:22:34 25 4
gpt4 key购买 nike

我正在为小型应用程序创建的 HashMap 上尝试使用 toString() 方法。该映射存储三个参数 aName aDate aTime .

但是,当我运行 for 循环时,它仅打印出映射的哈希代码,而不是每个键值对所需的文本表示形式。

for (Integer name: activities.keySet()) {
String key =name.toString();
String value = activities.get(name).toString();
System.out.println(key + " " + value);
}

我想知道这个函数哪里出了问题。任何指导将不胜感激。我的 2 门类(class)如下。

WhatsOn.java

import java.util.*;

public class WhatsOn {
public static void main(String[] args) {
WhatsOn alexa; // create an instance of the WhatsOn class
alexa = new WhatsOn();
alexa.addActivity("wash car","010117","0900");
alexa.addActivity("go shopping","020117","1000");
alexa.addActivity("return sale items","010117","1000");
alexa.addActivity("back to work", "040117", "0900");

for (Integer name: activities.keySet()) {
String key =name.toString();
String value = activities.get(name).toString();
System.out.println(key + " " + value);
}

}

private String today;
private int nextId;
private static Map<Integer, Activity> activities;

public WhatsOn() {
activities = new HashMap<Integer, Activity>();
today = "010117";
nextId = 1;
System.out.println("if you see this, the constructor is working");
}

// This method should create an instance of Activity class and then add it to the map referenced by the current value of nextId as the key
public void addActivity (String aName, String aDate, String aTime) {
Activity actToAdd = new Activity(aName, aDate, aTime); //create a new instance of the activity called actToAdd, this is a local var that stores methods arguments
activities.put(nextId, actToAdd); //Add this instance to your Map
nextId++; //increase the nextId
}
}

Activity .java

public class Activity {
private String name;
private String date;
private String time;

Activity(String name, String date, String time) {
this.name = name;
this.date = date;
this.time = time;
}

public void setDate(String aDate) {
this.date = aDate;
}

public void setTime(String aTime) {
this.time = aTime;
}

public void setName(String aName) {
this.name = aName;
}

public String getDate() {
return this.date;
}

public String getTime() {
return this.time;
}

public String getName() {
return this.name;
}
}

最佳答案

您需要覆盖Activity类中的toString方法。像这样的事情:

public String toString(){
return getName() + getDate() + getTime();
}

我建议您使用 IDE 的生成器功能来生成 toString 方法。 (另请参阅How to override toString() properly in Java?)

旁注

1)您对变量的命名可能不是最好的。当 Activity 的成员变量也称为“name”且实际上是一个 String 时,在 for 循环中命名 Integer“name”会令人困惑。

2) 在循环中,您再次访问 HashSet。这不是很优雅,因为您现在正在遍历 keySet。相反,考虑在循环时获取整个 EntrySet:

for (Map.Entry<Integer,Activity> entry: activities.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
System.out.println(key + " " + value);
}

关于java - toString 方法不打印 HashMap 对象的文本表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49819979/

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