gpt4 book ai didi

java - 如何打印出HashMap的值?输出如 xxx@da52a1

转载 作者:行者123 更新时间:2023-12-01 14:38:31 25 4
gpt4 key购买 nike

我有两个类。

Main.java

import java.util.HashMap;
import java.util.Map;

public class Main {
Map<Integer, Row> rows = new HashMap<Integer, Row>();
private Row col;

public Main() {
col = new Row();
show();
}

public void show() {
// col.setCol("one", "two", "three");
// System.out.println(col.getCol());

Row p = new Row("raz", "dwa", "trzy");
Row pos = rows.put(1, p);
System.out.println(rows.get(1));

}

public String toString() {
return "AA: " + rows;
}

public static void main(String[] args) {
new Main();
}
}

和 Row.java

public class Row {

private String col1;
private String col2;
private String col3;

public Row() {
col1 = "";
col2 = "";
col3 = "";
}

public Row(String col1, String col2, String col3) {
this.col1 = col1;
this.col2 = col2;
this.col3 = col3;
}

public void setCol(String col1, String col2, String col3) {
this.col1 = col1;
this.col2 = col2;
this.col3 = col3;
}

public String getCol() {
return col1 + " " + col2 + " " + col3;
}
}

输出总是看起来像“Row@da52a1”或类似的。如何解决这个问题?我希望能够通过轻松访问每个字符串来执行类似的操作:

str="string1","string2","string3"; // it's kind of pseudocode ;)
rows.put(1,str);
rows.get(1);

如您所见,我创建了 Row 类来将其用作 Map 的对象,但我不知道我的代码有什么问题。

最佳答案

toString 方法重写为 Row 类,如下所示:

@Override
public String toString() {
return col1 + " " + col2 + " " + col3;
}

关于java - 如何打印出HashMap的值?输出如 xxx@da52a1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16238254/

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