2014-03-28 17 views
0

下面是我的代碼爲我的DList接受對象。它打印出除了最後一個打印語句之外的所有內容。 「的System.out.println((int)的(list.head.next.next.next.item.getItem()));」這是爲什麼?以及如何解決它?從列表中訪問對象的方法。

/* DList1.java */ 

/** 
* A DList1 is a mutable doubly-linked list. (No sentinel, not 
* circularly linked.) 
*/ 

public class DList1 { 

    /** 
    * head references the first node. 
    * tail references the last node. 
    * 
    * DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS. 
    */ 

    protected DListNode1 head; 
    protected DListNode1 tail; 
    protected long size; 



    public DList1() { 
    head = null; 
    tail = null; 
    size = 0; 

    } 



    public DList1(Object a) { 
    head = new DListNode1(); 
    tail = head; 
    head.item = a; 
    size = 1; 
    } 



    public DList1(Object a, Object b) { 
    head = new DListNode1(); 
    head.item = a; 
    tail = new DListNode1(); 
    tail.item = b; 
    head.next = tail; 
    tail.prev = head; 
    size = 2; 
    } 

    public void insertFront(Object i) { 
    DListNode1 temp = new DListNode1(i); 
    if (size == 0) { 
     head = temp; 
     tail = temp; 
    } 
    else { 
     temp.next = head; 
     head.prev = temp; 
     head = temp; 
    } size++; 
    } 

    public void insertEnd(Object i) { 
    DListNode1 temp = new DListNode1(i); 
    if (size == 0) { 
     head = temp; 
     tail = temp; 
    } 
    else { 
     tail.next = temp; 
    temp.prev = tail; 
     tail = temp;  
    } size++; 
    } 

    public void removeFront() { 
    if (size == 0) { 
     return; 
    } 
    else if (size == 1) { 
     head = null; 
     tail = null; 
     size--; 
    } 
    else { 
     head = head.next; 
     head.prev = null; 
     size--; 
    } 
    } 


    public String toString() { 
    String result = "[ "; 
    DListNode1 current = head; 
    while (current != null) { 
     result = result + current.item + " "; 
     current = current.next; 
    } 
    return result + "]"; 
    } 

    public static void main(String[] args) { 
     int[] array = new int[2]; 
     array[0] = 3; 
     array[1] = 4; 
     int m = 1; 
     String g = "hi"; 
     String s = "boo"; 
     String z = "foo"; 
     DList1 list = new DList1(m); 
     tobject jim = new tobject(); 

     //list.insertFront(g); 
     list.insertEnd(s); 
     list.insertEnd(g); 
     list.insertEnd(jim); 
     System.out.println((list.head.item)); 
     System.out.println((list.head.next.item)); 
     System.out.println((list.head.next.next.item)); 
     System.out.println((int)(list.head.next.next.next.item.getItem())); 
     System.out.println(list.size); 
     //System.out.println((int)(list.head.next.item[0]));// expected 3 but failed 
     //System.out.println(((String) list.head.next.item)); 

    } 

} 
////////////// 
/* DListNode1.java */ 

/** 
* A DListNode1 is a node in a DList1 (doubly-linked list). 
*/ 

public class DListNode1{ 

    /** 
    * item references the item stored in the current node. 
    * prev references the previous node in the DList. 
    * next references the next node in the DList. 
    * 
    * DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS. 
    */ 

    public Object item; 
    public DListNode1 prev; 
    public DListNode1 next; 

    /** 
    * DListNode1() constructor. 
    */ 
    DListNode1() { 
    //item = NULL; 
    prev = null; 
    next = null; 
    } 

    DListNode1(Object i) { 
    item = i; 
    prev = null; 
    next = null; 
     } 
    } 
//////////// 
public class tobject { 
    private int pai; 

    public tobject(){ 
     pai = 3; 
    } 
    public int getItem(){ 
     return pai; 
    } 
} 
+1

項目是一個「對象」。它沒有任何'getItem()'方法。 – NeplatnyUdaj

+0

你在'insertend'中的其他部分看起來有點粗略?它將'temp'添加到'tail.next',然後將'temp.prev'設置爲'tail',然後使'tail'成爲'temp'? – Asthor

回答

0

的問題是,您需要先投你返回的值鍵入tobject之前,你可以叫你tobject定義的特殊getItem()方法。然後,您可以將結果投射到int,但println()足夠聰明,無需投射即可按「預期」方式打印任何Object或基元。

System.out.println(((tobject)list.head.next.next.next.item).getItem()); 

但是,通常試圖預先了解項目在列表內的類型太難以管理。這就是爲什麼generics可以派上用場的原因,但這確實意味着您應該嘗試將列表中的事物類型保留爲相同類型或子類型。

0

是否有原因將節點中的item定義爲Object? 商品是Object。它沒有任何getItem()方法。應該不是你的數據結構看起來更像是這個?:

public class DListNode1{ 
    public tobject item; 
    public DListNode1 prev; 
    public DListNode1 next; 
    ... 
    DListNode1(tobject i) { 
     item = i; 
     prev = null; 
     next = null; 
    } 
} 

//You should use uppercase to name class 
public class tobject { 
    ... 
} 

然後,你必須修復所有,如設置一個int,你真的想tobject的問題。

+0

雖然不是所有添加到「DList」的項目都屬於'tobject'類型,所以這是無效的 – Sam

+0

我現在看到了。這是一個瘋狂的代碼 – NeplatnyUdaj