java 如何从链接列表中的节点检索对象的内容
How to retrieve object's contents from a node within a Linked List in java
我创建了一个接受字符串和整数的 Person 对象。我创建了一个节点 class 以将泛型类型作为输入。当我调试时,我可以看到对象已创建并存储,但我的输出只是上述对象的内存地址。我尝试在 toString()
方法中将我的对象解析为字符串,但没有成功。我有一种预感,问题是我声明通用数据类型的方式。
这是我的代码:
Class 人
public class Person {
//variables
private String name;
private int age;
// default const.
public Person() {
name = " ";
age = 0;
}
// overloaded constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// methods
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Class节点
package LinkedList;
public class Node <T> {
// added parameters for person object
T p;
Node nextNode;
public Node () {
}
public Node (T p) {
this.p = p;
}
// parse int data into string to simplify the unit test by not showing node memory locations
@Override
public String toString() {
return p.toString();
}
}
链表class
package LinkedList;
public class myLinkedList <T>{
//Nodes
Node head;
Node tail;
int size = 0;
// Generic method
public void add (T p){
Node node = new Node(p);
if(tail == null){
head = node; // if no previous tail the new node is the head
tail = node; // if no previous tail the new node is also the tail
}else {
tail.nextNode = node; // adds a node to the end of the list
tail = node; // the newest node is set to the tail;
}
size ++;
}
// Generic search method
public Node search (T p) {
//empty list
if (head == null)
return null;
// check if the first node is a match
if (head.p == p)
return head;
// assign node as iterator
Node node = head;
// iterate through linked list
while (node.nextNode != null) {
// assign the next node as current node
node = node.nextNode;
// compare current node's data to data
if (node.p == p)
System.out.println(" object found");
return node;
//exit when the last node is reached
}
return null;
}
public Node findPreviousNode (T p) {
// if the current node has no previous node (it's the head)
if(head.p == p)
return new Node();
Node node = head;
//go through list looking for desired node and return the one before it
while(node.nextNode != null) {
if(node.nextNode.p == p)
return node;
// if desired node is not found move onto next node
node = node.nextNode;
}
// returns null for previous node if desired node doesn't exist
return null;
}
public Node delete (T p){
// node selected to delete, initializes as null for a placeholder
Node nodeToReturn = null;
//empty list nothing to return
if(size == 0)
return null;
//list of only one node
if(size == 1) {
nodeToReturn = head;
head = null;
tail = null;
size --;
return nodeToReturn;
}
// find and delete last node
Node nodePrev = findPreviousNode(p);
if(nodePrev.p == null) {
head = head.nextNode;
size --;
} else if(nodePrev != null) {
if(tail.p == p) {
nodePrev.nextNode = null;
tail = nodePrev;
} else {
nodePrev.nextNode = nodePrev.nextNode.nextNode;
}
size --;
}
return null;
}
public void traverse() {
// checks for empty list and prints out the first node
if(head != null) {
Node node = head;
System.out.print(node);
// moves from node to node printing until it reaches null as nextNode
while(node.nextNode != null) {
node = node.nextNode;
System.out.print( " " + node.toString());
}
}
}
}
主要Class
import java.util.ArrayList;
import LinkedList.*;
public class main {
public static void main(String[] args) {
// implement linked list
System.out.println("\n\n Object Linked list");
myLinkedList peopleGroup = new myLinkedList();
peopleGroup.add(new Person("robert", 23));
System.out.println(peopleGroup.search("robert"));
peopleGroup.add(mike);
peopleGroup.add(marie);
peopleGroup.traverse();
}
}
输出:
(我猜,罪魁祸首是搜索函数返回 null 的同样原因)。
Object Linked list
null
Person@677327b6 Person@14ae5a5 Person@7f31245a
Process finished with exit code 0
如果你想要更有意义的字符串,你需要在你的 Person 中覆盖 toString class。
您得到的输出不是内存地址而是散列。
toString()
的默认实现是 ClassName@hashValue
。
现在您将 class Node
中的 toString()
覆盖为 return Person.toString()
的值,这仍然是默认实现。
因此,如果您现在在 class Person
中覆盖 toString()
,您就可以开始了。
在你的代码中你有几个问题:
对于匹配你不应该依赖于 toString
方法。
在 search
方法中,您将输入作为 String
(名称)传递。并认为它是一个匹配,你有条件,如 (head.p == p)
和 if (node.p == p)
,其中 Node 是一个泛型类型(编译类型),但从技术上讲,它在运行时是 Person
对象。所以你在这里比较 String with Person Object
。这根本不会给你匹配。
如果要匹配,需要覆盖Person
Class和hashcode
的equals
方法。如下:
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (this.getClass() != obj.getClass()) return false;
Person that = (Person) obj;
if (this.age != that.getAge()) return false;
if (!this.name.equals(that.getName())) return false;
return true;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + name.hashCode();
result = 31 * result + age;
return result;
}
您可以根据您的要求和搜索条件覆盖这些方法。但是您需要更改 search
方法中的代码,如下所示:
// Generic search method
public Node search (T p) {
//empty list
if (head == null)
return null;
// check if the first node is a match
if (head.p.equals(p))
return head;
// assign node as iterator
Node node = head;
// iterate through linked list
while (node.nextNode != null) {
// assign the next node as current node
node = node.nextNode;
// compare current node's data to data
if (node.p.equals(p))
System.out.println(" object found");
return node;
//exit when the last node is reached
}
return null;
}
并且当您从搜索中传递 Person 时,您需要按如下方式传递 Person 对象:
peopleGroup.search(new Person("robert", 23));
所以在这些更改之后你会得到结果,比如(跳过添加迈克和玛丽):
Object Linked list
test.file1.Person@518532a6
test.file1.Person@518532a6 test.file1.Person@6319ab2 test.file1.Person@bf8ccc2e
我创建了一个接受字符串和整数的 Person 对象。我创建了一个节点 class 以将泛型类型作为输入。当我调试时,我可以看到对象已创建并存储,但我的输出只是上述对象的内存地址。我尝试在 toString()
方法中将我的对象解析为字符串,但没有成功。我有一种预感,问题是我声明通用数据类型的方式。
这是我的代码:
Class 人
public class Person {
//variables
private String name;
private int age;
// default const.
public Person() {
name = " ";
age = 0;
}
// overloaded constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// methods
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Class节点
package LinkedList;
public class Node <T> {
// added parameters for person object
T p;
Node nextNode;
public Node () {
}
public Node (T p) {
this.p = p;
}
// parse int data into string to simplify the unit test by not showing node memory locations
@Override
public String toString() {
return p.toString();
}
}
链表class
package LinkedList;
public class myLinkedList <T>{
//Nodes
Node head;
Node tail;
int size = 0;
// Generic method
public void add (T p){
Node node = new Node(p);
if(tail == null){
head = node; // if no previous tail the new node is the head
tail = node; // if no previous tail the new node is also the tail
}else {
tail.nextNode = node; // adds a node to the end of the list
tail = node; // the newest node is set to the tail;
}
size ++;
}
// Generic search method
public Node search (T p) {
//empty list
if (head == null)
return null;
// check if the first node is a match
if (head.p == p)
return head;
// assign node as iterator
Node node = head;
// iterate through linked list
while (node.nextNode != null) {
// assign the next node as current node
node = node.nextNode;
// compare current node's data to data
if (node.p == p)
System.out.println(" object found");
return node;
//exit when the last node is reached
}
return null;
}
public Node findPreviousNode (T p) {
// if the current node has no previous node (it's the head)
if(head.p == p)
return new Node();
Node node = head;
//go through list looking for desired node and return the one before it
while(node.nextNode != null) {
if(node.nextNode.p == p)
return node;
// if desired node is not found move onto next node
node = node.nextNode;
}
// returns null for previous node if desired node doesn't exist
return null;
}
public Node delete (T p){
// node selected to delete, initializes as null for a placeholder
Node nodeToReturn = null;
//empty list nothing to return
if(size == 0)
return null;
//list of only one node
if(size == 1) {
nodeToReturn = head;
head = null;
tail = null;
size --;
return nodeToReturn;
}
// find and delete last node
Node nodePrev = findPreviousNode(p);
if(nodePrev.p == null) {
head = head.nextNode;
size --;
} else if(nodePrev != null) {
if(tail.p == p) {
nodePrev.nextNode = null;
tail = nodePrev;
} else {
nodePrev.nextNode = nodePrev.nextNode.nextNode;
}
size --;
}
return null;
}
public void traverse() {
// checks for empty list and prints out the first node
if(head != null) {
Node node = head;
System.out.print(node);
// moves from node to node printing until it reaches null as nextNode
while(node.nextNode != null) {
node = node.nextNode;
System.out.print( " " + node.toString());
}
}
}
}
主要Class
import java.util.ArrayList;
import LinkedList.*;
public class main {
public static void main(String[] args) {
// implement linked list
System.out.println("\n\n Object Linked list");
myLinkedList peopleGroup = new myLinkedList();
peopleGroup.add(new Person("robert", 23));
System.out.println(peopleGroup.search("robert"));
peopleGroup.add(mike);
peopleGroup.add(marie);
peopleGroup.traverse();
}
}
输出: (我猜,罪魁祸首是搜索函数返回 null 的同样原因)。
Object Linked list
null
Person@677327b6 Person@14ae5a5 Person@7f31245a
Process finished with exit code 0
如果你想要更有意义的字符串,你需要在你的 Person 中覆盖 toString class。
您得到的输出不是内存地址而是散列。
toString()
的默认实现是 ClassName@hashValue
。
现在您将 class Node
中的 toString()
覆盖为 return Person.toString()
的值,这仍然是默认实现。
因此,如果您现在在 class Person
中覆盖 toString()
,您就可以开始了。
在你的代码中你有几个问题:
对于匹配你不应该依赖于 toString
方法。
在 search
方法中,您将输入作为 String
(名称)传递。并认为它是一个匹配,你有条件,如 (head.p == p)
和 if (node.p == p)
,其中 Node 是一个泛型类型(编译类型),但从技术上讲,它在运行时是 Person
对象。所以你在这里比较 String with Person Object
。这根本不会给你匹配。
如果要匹配,需要覆盖Person
Class和hashcode
的equals
方法。如下:
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (this.getClass() != obj.getClass()) return false;
Person that = (Person) obj;
if (this.age != that.getAge()) return false;
if (!this.name.equals(that.getName())) return false;
return true;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + name.hashCode();
result = 31 * result + age;
return result;
}
您可以根据您的要求和搜索条件覆盖这些方法。但是您需要更改 search
方法中的代码,如下所示:
// Generic search method
public Node search (T p) {
//empty list
if (head == null)
return null;
// check if the first node is a match
if (head.p.equals(p))
return head;
// assign node as iterator
Node node = head;
// iterate through linked list
while (node.nextNode != null) {
// assign the next node as current node
node = node.nextNode;
// compare current node's data to data
if (node.p.equals(p))
System.out.println(" object found");
return node;
//exit when the last node is reached
}
return null;
}
并且当您从搜索中传递 Person 时,您需要按如下方式传递 Person 对象:
peopleGroup.search(new Person("robert", 23));
所以在这些更改之后你会得到结果,比如(跳过添加迈克和玛丽):
Object Linked list
test.file1.Person@518532a6
test.file1.Person@518532a6 test.file1.Person@6319ab2 test.file1.Person@bf8ccc2e