我有2個班。如何打印出HashMap值?輸出像xxx @ da52a1
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;
}
}
輸出總是看起來像 「行@ da52a1」 或類似。如何解決這個問題?我希望能夠做這樣的事,方便前往各字符串:
str="string1","string2","string3"; // it's kind of pseudocode ;)
rows.put(1,str);
rows.get(1);
正如你所看到的,我創建的類行利用其作爲地圖的對象,但我不知道是什麼我的代碼有問題。
@Baadshah我有這個值,所以我認爲它運作良好。謝謝durron597 :) – tmq 2013-04-26 14:02:10
@tmq沒問題,不要忘了點擊綠色的複選標記:) – durron597 2013-04-26 14:02:56
@tmq這就是我給durron +1的原因:) – 2013-04-26 14:05:37