У меня вопрос, как преобразовать объект в строку, в исходный код здесь вы можете видеть, что вы можете вводить объект Object в качестве объекта, однако я бы хотел, чтобы элемент был преобразуется в строку, поэтому я могу сопоставить ее с строковой переменной. Кто-нибудь знает, как это сделать?Преобразование объекта в строку (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