我有一個問題如何在源代碼中將對象項轉換爲字符串 這裏您可以看到您可以將對象項作爲對象輸入,但是我希望項目是轉換成一個字符串,所以我可以將它等同於一個字符串變量。 有誰知道如何做到這一點?將對象轉換爲字符串(java)
謝謝
public QueueNode AddItem (Object item, int priority)
{
PQNode newNode = new PQNode (item, priority);
if (root != null) {
spreadingOutToInsert (newNode);
// if newNode equals or is greater than the root then put the old root as the rightChild of the newnode
if (newNode.compare (root) < 0)
{
newNode.leftNode = root.leftNode;
newNode.right = root;
root.leftNode = null;
// if newNode equals or is greater than the root then just put the old root as the leftChild of the newnode
} else
{
newNode.leftNode = root;
newNode.right = root.right;
root.right = null;
};
};
size++;
return root = newNode; // this is to make the newNode into the new root
};
你不能簡單地寫一個方法嗎?手動或[覆蓋toString()](https://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java)。 – Klaster 2015-02-08 22:58:32