2012-05-09 76 views
-1

OK so搜索是我在Java中的薄弱環節,可以真正使用幫助,從哪裏開始這項任務! 數據成員n不再需要。 LinkedList應該在構造函數中創建並分配給列表,而不是在run()中。 makeScanner(),getPerson()和main()不應該被修改。 Person和FileFormatException類也不應該被修改。由於列表不再是數組,因此display()將不再編譯。您可以將其更改爲使用foreach或僅將其刪除。 run()有一個將Person對象添加到數組的循環。改變它,以便將它們添加到列表中。考慮:搜索工作分配難度...鏈接列表

theList.add(p); 

變量index和n不再是必需的。 修改search()以對列表而非數組執行線性搜索。最簡單的方法是使用foreach並返回正確的Person(如果找到)。如果沒有找到正確的Person,它應該像以前一樣返回null。

這是我到目前爲止有:

import java.util.LinkedList; 
import java.io.IOException; 
import java.net.URL; 
import java.util.Scanner; 


public class ContactList { 

private LinkedList<Person> theList; 

private int n;   // the number of Persons in theList 
private Scanner keyboard; 

public ContactList() { 
keyboard = new Scanner(System.in); 
} // no-arg constructor 

// Returns a Scanner associated with a specific text-based URL 
// online. 
private Scanner makeScanner() throws IOException { 
final String source = 
    "http://userpages.umbc.edu/~jmartens/courses/is247/hw/05/05.txt"; 
final URL src = new URL(source); 
return new Scanner(src.openStream()); 
} // makeScanner() 


// Return a Person instance based upon data read from the given 
// Scanner. 
private Person getPerson(final Scanner in) throws FileFormatException { 
if (!in.hasNextLine()) 
    return null; 

String line = in.nextLine().trim(); 
int key = Integer.parseInt(line); 
String name = in.nextLine().trim(); 
String mail = in.nextLine().trim().toLowerCase(); 
if (in.hasNextLine()) { 
    String empty = in.nextLine().trim(); // skip blank line 
    if (empty.length() > 0) 
    throw new FileFormatException("missing blank line"); 
} // if 

return new Person(key, name, mail); 
} // getPerson() 


// Display the array contents. 
private void display() { 
for (int i = 0; i < n; ++i) 
    System.out.println(theList[i]); 
} // display() 


// Read the Person objects from the web page and then start the user 
// interface. 
private void run() throws IOException { 
theList = new Person[1024]; 
try { 
    Scanner in = makeScanner(); 

    int index = 0; 
    Person p = getPerson(in); 
    while (p != null) { 
    theList[index++] = p; 
    p = getPerson(in); 
    } 
    n = index; 
} catch (IOException e) { 
    System.err.println("Error reading web page: " + e); 
    System.exit(1); 
    // The call to exit may be overkill, but it is nice to return an 
    // error (nonzero) value to the environment. Since main() does 
    // nothing after run() returns, simply returning to main() would 
    // be acceptable also. Perhaps the easiest way to do this is to 
    // simply move the call to ui() below into the try block. Then if 
    // an exception is thrown, the UI never executes. 
} // catch 

// Run the user interface. 
    ui(); 
// display(); 
} // run() 

// Loop prompting the user for an integer key. Terminate on a negative 
// key. If a record matching the key is found, display the 
// record. Otherwise, indicate that no matching record was found. 
private void ui() { 
int key = getKey(); 
while (key >= 0) { 
    Person p = search(key); 
    if (p == null) 
    System.out.println("No person matching key " 
         + key 
         + " found."); 
    else 
    System.out.println(p); 
    key = getKey(); 
} // while not done 
} // ui() 

private int getKey() { 
System.out.print("\nPlease enter a key: "); 
int key = keyboard.nextInt(); 
return key; 
} // getKey() 

private Person search(final int key) { 
for (int index = 0; index < n; ++index) 
    if (key == theList[index].getId()) // Is this the right one? 
    return theList[index]; 

return null;  // apparently the requested object is not present 
} // search() 

public static void main(String[] args) throws IOException { 
ContactList cl = new ContactList(); 
cl.run(); 
} // main() 

} // class ContactList 
+4

你是否從概念上理解LinkedList是如何工作的?如果不是這樣的話,那就開始吧。 – Charles

+0

你的問題是什麼?這個代碼是否真的需要具體的問題? – amit

回答

3

我會做的第一件事就是改變你的表聲明! (像你說的)

變化:

private Person[] theList; 

private LinkedList<Person> theList; 

然後用你的編譯器來打印你的所有的編譯錯誤或看看你的IDE產生所有的紅色波浪線。

在存在編譯錯誤或紅色波形的每個點上,確定嘗試進行的陣列操作。然後在此頁面上搜索相應的正確操作或操作順序。 http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html