LinkedList Java przechodzi i drukuje
Byłbym bardzo wdzięczny, jeśli możesz pomóc w odpowiedzi na to pytanie:
Sama niestandardowa lista powiązań została już utworzona w bardzo standardowy sposób przy użyciu języka Java. Poniżej moje zajęcia:
public class Node {
private Object obj;
private Node next;
public Node(Object obj){
this(obj,null);
}
public Node(Object obj,Node n){
this.obj = obj;
next = n;
}
public void setData(Object obj){
this.obj = obj;
}
public void setNext(Node n){
next = n;
}
public Object getData(){
return obj;
}
public Node getNext(){
return next;
}
}
public class linkedList {
private Node head;
public linkedList(){
head = null;
}
public void setHead(Node n){
head = n;
}
public Node getHead(){
return head;
}
public void add(Object obj){
if(getHead() == null){
Node tmp = new Node(obj);
tmp.setNext(getHead());
setHead(tmp);
}else{
add(getHead(),obj);
}
}
private void add(Node cur,Object obj){
if(cur.getNext() == null){
Node tmp = new Node(obj);
tmp.setNext(cur.getNext());
cur.setNext(tmp);
}else{
add(cur.getNext(),obj);
}
}
}
Próbuję wydrukować wartość, którą umieściłem na liście, jak poniżej
public static void main(String[] args) {
// TODO code application logic here
Node l = new Node("ant");
Node rat = new Node("rat");
Node bat = new Node("bat");
Node hrs = new Node("hrs");
linkedList lst = new linkedList();
lst.add(l);
lst.add(rat);
lst.add(bat);
lst.add(hrs);
Node tmp = lst.getHead();
while(tmp != null){
System.out.println(tmp.getData());
tmp = tmp.getNext();
}
}
ale dane wyjściowe otrzymane z IDE to
linklist.Node@137bd6a1
linklist.Node@2747ee05
linklist.Node@635b9e68
linklist.Node@13fcf0ce
dlaczego drukuje odniesienie, ale nie rzeczywistą wartość ciągu, taką jak bat, mrówka, szczur ...?
Jeśli chcę wydrukować rzeczywistą wartość, co powinienem zrobić?
Dziękuję Ci bardzo