2013-04-17 113 views
0

我在LinkList.java中的「curr.names.length() - 1」中出錯。 .length是一個錯誤。我該如何解決?這裏還是新的,我不太瞭解Java。只是覺得我會試一試。這是我得到的代碼:在鏈表上實現氣泡排序

SinglyNode.java

public class SinglyNode 
{ 
public Object names; 
public SinglyNode next; 

public SinglyNode (Object name1) 
{ 
    names = name1; 
    next = null; 
} 

public void setNext(SinglyNode next1) 
{ 
    next = next1; 
} 

Object getObject() 
{ 
    return names; 
} 

SinglyNode getNext() 
{ 
    return next; 
} 

void displayLink() 
{ 
    System.out.print("{" + names + "}"); 
} 
} 

LinkList.java的錯誤是在這裏

public class LinkList 
{ 
SinglyNode first; 
SinglyNode last; 

public boolean isEmpty() 
{ 
    return (first == null); 
} 

void insertFirst(Object name1) 
{ 
    SinglyNode newNode1 = new SinglyNode(name1); 
    newNode1.next = first; 
    first = newNode1; 
} 

void display() 
{ 
    SinglyNode current = first; 
    while(current != null) 
    { 
     current.displayLink(); // print data 
     current = current.next; // move to next link 
    } 
    System.out.println("\n"); 
} 

public void bubbleSort() 
{ 
    int length = 0; 
    SinglyNode hold = first; 
    while(true) 
    { 
     if(hold == last) 
     { 
      break; 
     } 
     hold = hold.next; 
     length++; 
    } 
    while(true) 
    { 
     if(length == 0) 
     { 
      break; 
     } 
     int i = 0; 

     SinglyNode curr = first; 
     while(true) 
     { 
      if(i == length) 
      { 
       break; 
      } 
      if(curr.names.charAt(curr.names.length()-1) > 
        curr.next.names.charAt(curr.next.names.length()-1)) 
      { 
       swap(curr); 
      } 
      curr = curr.next; 
      i++; 
     } 
     length--; 
    } 
} 

private void swap(SinglyNode node) 
{ 
    Object temp = node.names; 
    node.names = node.next.names; 
    node.next.names = temp; 
} 
} 

而這裏的主類 MainApp.java

ppublic class MainApp 
{ 
public static void main (String args[]) 
{ 
    LinkList list = new LinkList(); 

    list.insertFirst("Squirtle"); 
    list.insertFirst("Bulbasaur"); 
    list.insertFirst("Charmander"); 
    list.insertFirst("Pichu"); 
    list.insertFirst("Ghastly"); 
    list.insertFirst("Mewtwo"); 
    list.insertFirst("Dialga"); 

    System.out.println("LIST: "); 
    list.display(); 

    System.out.println("BUBBLE SORT: "); 
    list.bubbleSort(); 
    list.display(); 

} 
} 
+3

什麼是錯誤?它看起來像'名稱'成員是'對象'類型。 'Object'是否有一個叫'length()'的成員?我不認爲它:http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html – David

回答

0

Object沒有length()方法 - 只能使用適當的方法簽名在類上調用.length()。你打算這是一個String?如果是,請將您的代碼更改爲public String names,public SinglyNode(String name1)String getObject()(或String getName()String getString()

+0

我做了,但然後「node.next.names = temp; 「在LinkList中出現錯誤 – lmpgdn

+0

將'Object temp'更改爲'String temp'。將所有對象都更改爲字符串。如果沒有首先投射對象,則不能將對象分配給字符串,例如'node.next.names =(String)temp';然而,在這種情況下,暫時沒有理由首先成爲對象。 –

+0

好的,我明白了。但是,然後我得到這個錯誤「線程中的異常」main「java.lang.NullPointerException」它在「if(curr.names.charAt(curr.names.length() - 1)>」行 – lmpgdn