은 이미 자바를 사용하여 매우 표준적인 방법으로 사용자 정의 연결리스트 나 자신을 만들었습니다. 다음은 내 클래스는 다음과 같습니다
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);
}
}
}
임 내가
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();
}
}
다음과 같이 목록에 삽입했지만 내가 IDE에서 가져온 출력이
[email protected]
[email protected]
[email protected]
[email protected]
않는 이유는 값을 인쇄하려고 박쥐, 개미, 쥐 같은 문자열의 실제 값이 아니라 참조를 출력합니다 ...?
실제 값을 인쇄하려면 어떻게해야합니까?
당신에게
이것은 제쳐두고,하지만 제네릭을 사용하지 않는 이유는 무엇입니까? –
당신은 당신의 Node 클래스에 toString() 메소드를 추가 할 수 있습니다. 그러면 잘 인쇄 될 것입니다. – Kunal