2012-10-18 157 views
1

我有一個任務來創建一個鏈表並分成兩個子列表 ,現在我的代碼有錯誤。如果任何人可以幫助我的代碼,我不知道哪裏需要改變,因爲我改變了一些它給了我更多的錯誤。Java分割列表

public class UnorderedLinkedList<E extends Comparable<? super E>> extends LinkedList<T> 
{ 
    public void splitMid(LinkedList<String> subList) 
    { 
     LinkedList<T> current;//the head pointer 
     LinkedList<T> mid;//the mid point 

     //Node first = firstNode; 
     //Node last = firstNode; 

     //Node subListFirst; 
     //Node subListLast; 
     int i; 

     if(head == null) 
     { 
      subList.head = null; 
      subList.last = null; 
      subList.count = 0; 
     } 
     else 
     { 
      //mid = 
      head = null; 
      current = head.next; 
      i = 1; 

      if(current != null) 
       current = current.next; 

      while(current != null) 
      { 
       mid = mid.next; 
       current = current.next; 
       i++; 

       if(current != null) 
        current = current.next; 
      } 

      subList.head = head.next; 
      subList.last = last; 
      last = mid ; 
      last.next = null; 

      subList.count = count - i; 
      count = i; 
     } 
    } 
} 

錯誤消息

G:\LinkedList\src\LinkedList.java:184: error: cannot find symbol subList.count = 0;

symbol: variable count.

location: variable subList of type LinkedList.Node

where T,E are type-variables:

T extends Object declared in class LinkedList.

E extends Comparable declared in class LinkedList.UnorderedLinkedList

我的主類:

public void main(String args[]) 
{ 
    LinkedList<Integer> myList = new LinkedList<Integer>(); 
    LinkedList<Integer> subList = new LinkedList<Integer>(); 

    myList.addLast(34); 
    myList.addLast(65); 
    myList.addLast(87); 
    myList.addLast(29); 
    myList.addLast(12); 

    myList.splitMid(subList); 
} 

錯誤信息

G:\LinkedList\src\LinkedTest.java:31: error: cannot find symbol.

myList.splitMid(subList);

symbol: method splitMid(LinkedList)

location: variable myList of type LinkedList

+0

什麼烏爾meaing關於 「分成兩個子列表」 ??? – zetsin

回答

1

關於編譯錯誤:

您正在使用的LinkedList實例調用你爲類UnorderedLinkedList

0

1.

G:\LinkedList\src\LinkedList.java:184: error: cannot find symbol subList.count = 0; 

嘗試刪除subList.count定義的方法;它沒有必要爲計數賦值;

2.

G:\LinkedList\src\LinkedTest.java:31: error: cannot find symbol. 

myList.splitMid(subList); 

做到這一點:

UnorderedLinkedList<Integer> myList = new UnorderedLinkedList<Integer>(); 
+0

請注意,'myList'需要鍵入爲'UnorderedLinkedList',以便調用'splitMid'。 –

+0

行,即時通訊錯誤... – zetsin

+0

'myList.splitMid(subList);'不會編譯,因爲'myList'靜態類型爲'LinkedList',它沒有聲明'splitMid'。 'myList'必須聲明爲'UnorderedLinkedList'。 –