我有一個TreeMap,我將Integer指定爲鍵,將Student指定爲其值。有沒有辦法打印學生類型的確切名稱?檢索樹形圖的值不檢索對象名稱
例如,在驅動程序類我將實例學生爲:
Student two = new Student(1002,"two" );
是否有針對的get()函數或pollFirstEntry)的方式(返回實例化Student對象的實際名稱,即「兩個」?而不是學生@ XXXXXXXXX。
import java.util.TreeMap;
public class Table <K extends Comparable<K>, T> // K = key, T = item
{
TreeMap<K,T> tree = new TreeMap<K,T>();
TreeMap<K,T> treeclone = new TreeMap<K,T>();
public boolean isEmpty()
{
if (tree.size()>0)
return false;
return true;
}
public int size()
{
return tree.size();
}
public T tableRetrieve(K key)
{
return tree.get(key);
}
@SuppressWarnings("unchecked")
public void printTable()
{
treeclone = (TreeMap<K, T>)tree.clone();
int n = treeclone.size();
for (int i = 0; i<n; i++)
{
System.out.println(treeclone.pollFirstEntry());
}
}
}
輸出使用調用功能
1003: [email protected]
輸出使用PRINTTABLE功能
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
您需要重寫'Student'類中的'toString()'方法。 'toString()的默認實現由['java.lang.Object'](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html)確定,它打印出什麼你正在看到。 – 2014-09-05 04:36:07